Memcpy无法使用unicode [英] Memcpy is not working in unicode
问题描述
无法在unicode模式下将数据从m_pdata复制到pdata。
m_pdata - LPBTYE
pdata- LPTSTR >
nlen =是m_pdata的长度
void CXTest :: Load(CString& val)
{
LPTSTR pBuffer = val.GetBuffer(val.getlength()+ 1);
memcpy(pdata,m_pdata,nLen);
pdata [nLen] = _T('\ 0');
val.ReleaseBuffer();
}
pdata正在获得垃圾值。
请告诉我它为什么不工作。
我尝试过:
m_pdata有数据
LPTSTR pBuffer = val。 GetBuffer(val.getlength()+ 1);
memcpy(pdata,m_pdata,nLen);
pdata [nLen] = _T('\0');
val.ReleaseBuffer();
Not able to copy data from m_pdata to pdata in the unicode mode.
m_pdata - LPBTYE
pdata- LPTSTR
nlen = is length of m_pdata
void CXTest::Load(CString &val)
{
LPTSTR pBuffer = val.GetBuffer(val.getlength() + 1);
memcpy(pdata, m_pdata, nLen);
pdata[nLen] = _T('\0');
val.ReleaseBuffer();
}
pdata is getting junk value.
Please let me know why it is not working.
What I have tried:
m_pdata is having data
LPTSTR pBuffer = val.GetBuffer(val.getlength() + 1);
memcpy(pdata, m_pdata, nLen);
pdata[nLen] = _T('\0');
val.ReleaseBuffer();
推荐答案
当你有nLen
字节时m_pdata
这些不是Unicode字符,而是一些ki nd的ANSI / ASCII字符,你想创建一个Unicode字符串,使用 MultiByteToWideChar功能(Windows) [ ^ ]:
When you havenLen
bytes inm_pdata
that are not Unicode characters but some kind of ANSI / ASCII characters and you want to create a Unicode string from those use the MultiByteToWideChar function (Windows)[^]:
if (nLen < nWideSize - 1)
{
MultiByteToWideChar(nCodePage, 0, (LPCSTR)m_pdata, nLen, pdata, nWideSize);
pdata[nLen] = _T('\0');
}
其中
nCodePage
=中的字符编码 m_pdata
nWideSize
= pdata的大小
字符缓冲区
当字节为ASCII时,使用代码页20127.对于其他代码页,请参阅代码页标识符(Windows) [ ^ ]。
where
nCodePage
= the encoding of the characters in m_pdata
nWideSize
= the size of the pdata
buffer in characters
When the bytes are ASCII, use code page 20127. For other code pages see Code Page Identifiers (Windows)[^].
更新版本的MFC使用ATL的CString类 - 可以在ANSI和Unicode之间进行转换。
如何:在各种字符串类型之间进行转换 [ ^ ]
就像TCHAR一样是char或wchar_t,CString实际上可能是CStringA或CStringW。无论哪种情况,将const char *分配给CStringA或CStringW都可以正常工作 - 如上面的MSDN文章所示。
这是我的假设:
The more recent versions of MFC use the CString classes from ATL - which can convert between ANSI and Unicode.
How to: Convert Between Various String Types[^]
Just as TCHAR may be char or wchar_t, CString may actually be CStringA or CStringW. In either case, assigning a const char * to a CStringA or CStringW just works - as shown by the MSDN article above.
Here's my assumptions:
class CXTest
{
unsigned char m_pdata[1024]; // pointer to byte?
unsigned nLen;
void Load(CString &val);
};
如果您的字符串是nul终止,这将有效。
If your string is nul terminated, this would work.
void CXTest::Load(CString &val)
{
const char *text = (const char *)m_pdata;
val = text;
}
你的文字有一个长度所以你可能想要这个:
Your text has a length so you probably want this:
void CXTest::Load(CString &val)
{
const char *text = (const char *)m_pdata;
CString load(text, nLen);
val = load;
}
这篇关于Memcpy无法使用unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!