本文介绍了Cstring到字节和字节到cbytearray转换-MFC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图在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﵄﵄ 解决方案 因为你使用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﵄﷽ꮫꮫ" 解决方案 这篇关于Cstring到字节和字节到cbytearray转换-MFC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!