Cstring到字节和字节到cbytearray转换-MFC [英] Cstring to byte and byte to cbytearray conversion-MFC

查看:496
本文介绍了Cstring到字节和字节到cbytearray转换-MFC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在MFC中将CString转换为BYTE *和BYTE *转换为CByteArray,CString已转换为BYTE *。但是我无法将整个字节转换为CByteArray它返回带有一些垃圾值的部分数据。我在这里描述了我的实际问题...



我尝试了什么:



 CString csData = _T(someData); 

BYTE * pByteArray =(PBYTE)(LPCTSTR)csData.GetBuffer();

CString str;
str = LPTSTR(pByteArray);
AfxMessageBox(str); //返回someData

CByteArray arrByte2;

arrByte2.SetSize(csData.GetLength()+ 1);

memcpy(arrByte2.GetData(),pByteArray,csData.GetLength()+ 1);

CString text((LPTSTR)arrByte2.GetData(),arrByte2.GetSize());
CStringA结果(文字);
AfxMessageBox(text); //返回some﵄﵄

解决方案

A CString TCHAR 字符的序列。 TCHAR char wchar_t ,具体取决于项目字符集设置(ANSI /多字节或Unicode)。



您可以将指针强制转换为 CString BYTE *

  const  BYTE * pByte = reinterpret_cast< const BYTE *>(str.GetString()); 

请注意,我在这里使用了C ++,并使用 const 因为 CString :: GetString()的返回类型是 LPCTSTR 。如果你真的需要一个非常量指针(想要修改 CString ),你可以使用 CString :: GetBuffer()但之后你必须调用 CString :: ReleaseBuffer()



如果你有一个Unicode版本,字符串中的字符数与字节数不同。复制到字节数组时必须考虑到这一点。然后也不需要 BYTE * 指针变量:

 CByteArray arrByte2; 
// CString中的字节数,包括终止的NULL字节
size_t byteSize =(str.GetLength()+ 1 )* sizeof (TCHAR);
arrByte2.SetSize(byteSize);
// 无需在此处投射,因为memcpy()参数的类型为void *
memcpy(arrByte2.GetData(),str.GetString(),byteSize);

现在字节数组包含字符串中的所有字节,包括终止的NULL字节,可以分配回来a CString

 CString文本(reinterpret_cast< LPCTSTR>(arrByte2.GetData()),arrByte2.GetSize ()/  sizeof (TCHAR)); 

请注意,现在必须通过除以字符大小来计算字符数。这里也没有必要在数组中有一个终止的NULL字节,因为 CString 接受一个长度参数的构造函数会追加它。


< blockquote>因为你使用UNICODE,所以字符串的大小为2字节,所以你的缓冲区很小。所以你加倍了CByteArray的缓冲区siz。

 arrByte2.SetSize( 2  *(csData.GetLength()+ 1)); 



顺便说一句:你已经正确地看了它,但没有把它理解为错误提示; - )


I have tried to convert a CString To BYTE* and "BYTE* to CByteArray" in MFC,The CString Has Been Converted To BYTE*. But I'm not able to Convert The Entire Byte* To CByteArray It Returns Partial Data With Some Garbage Values. I Described My Actual Problem Here...

What I have tried:

CString csData =_T("someData");

BYTE *pByteArray = (PBYTE)(LPCTSTR)csData.GetBuffer();

CString str;
str=LPTSTR(pByteArray);
AfxMessageBox(str); //returns "someData" 

CByteArray arrByte2;

arrByte2.SetSize(csData.GetLength()+1);

memcpy(arrByte2.GetData(), pByteArray, csData.GetLength()+1);

CString text((LPTSTR)arrByte2.GetData(),arrByte2.GetSize());
CStringA result(text);
AfxMessageBox(text);//returns "some﵄﷽꯽ꮫꮫ"

解决方案

A CString is a sequence of TCHAR characters. A TCHAR is a char or a wchar_t depending on the project character set setting (ANSI/multi-byte or Unicode).

You can cast the pointer to the data in the CString to BYTE*:

const BYTE *pByte = reinterpret_cast<const BYTE*>(str.GetString());

Note that I have used C++ casting here and use const because the CString::GetString() return type is LPCTSTR. If you really need a non const pointer (want to modify the CString) you can use CString::GetBuffer() but then you have to call CString::ReleaseBuffer() afterwards.

If you have a Unicode build, the number of characters in the string is not identical to the number of bytes. You have to take that into account when copying to the byte array. Then there is also no need for a BYTE* pointer variable:

CByteArray arrByte2;
// The number of bytes in the CString including the terminating NULL bytes
size_t byteSize = (str.GetLength() + 1) * sizeof (TCHAR);
arrByte2.SetSize(byteSize);
// No need to cast here because the memcpy() parameters are of type void*
memcpy(arrByte2.GetData(), str.GetString(), byteSize);

Now the byte array contains all the bytes from the string including the terminating NULL bytes and can be assigned back to a CString:

CString text(reinterpret_cast<LPCTSTR>(arrByte2.GetData()), arrByte2.GetSize() / sizeof(TCHAR));

Note that the number of characters has to be calculated now by dividing by the size of a character. It would be also not necessary to have a terminating NULL byte in the array here because the CString constructors accepting a length parameter will append that.


Because you use UNICODE the string has 2 byte size and so your buffer is to small. So you double the buffer siz of the CByteArray.

arrByte2.SetSize(2*(csData.GetLength()+1));


BTW: You have watched it correctly, but didnt understand it as error hint ;-)


这篇关于Cstring到字节和字节到cbytearray转换-MFC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆