CString型数据转换成BYTE型数据

来源:互联网 发布:手机淘宝怎么申请帐号 编辑:程序博客网 时间:2024/06/06 19:19

最近一直在处理数据,发现在处理CString型数据转换成BYTE型数据时总出错,而且在存文件时,也出现存的不对,这应该是字符长度的原因,就是宽字符那些问题。。。 今天 停下手中的活,整理了一下,如下:

 CString str = L"abcdefg";
 DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,str,-1,NULL,0,NULL,FALSE);
 BYTE* psText;
 psText = new BYTE[dwNum];
 if(!psText)
 {
  delete []psText;
 }
 WideCharToMultiByte(CP_OEMCP,NULL,str,-1,(LPSTR)psText,dwNum,NULL,FALSE);
 BYTE by;
 for(int i=0;i<dwNum;i++)
 {
  by = psText[i];
 }

 CFile file(L"E://Test.txt",CFile::modeCreate|CFile::modeWrite);
 //file.Write(str,str.GetLength());
 file.Write(psText,dwNum);
 file.Close();

 delete []psText;

 

如果用file.Write(str,str.GetLength()); 写入文件的内容 肯定出错, 就是因为字符长度的原因

原创粉丝点击