当前位置: 代码迷 >> WinCE >> WINCE系统中,CString含中文,怎么转成char * , 通过串口输出
  详细解决方案

WINCE系统中,CString含中文,怎么转成char * , 通过串口输出

热度:176   发布时间:2016-04-28 13:49:24.0
WINCE系统中,CString含中文,如何转成char * , 通过串口输出?
试了N个办法,请大家在[看清前提]下出出主意帮帮忙:


1. 项目属性的Locale设置的是美国英文
2. 项目字符集是 unicode
3. 是WINCE系统,不是电脑的WINDOW

如果我在程序中通过如下语句:
printf("\n=========================================\n");
printf("测试结果:\n");

电脑上面串口工具接收和显示的结果一切正常,"测试结果"这几个中文是没问题的!!!!

但如下代码就有问题:

  CString m_stringDisp = _T("abc中文");

  int n = m_stringDisp.GetLength();
  int len = WideCharToMultiByte(CP_ACP,0,m_stringDisp,m_stringDisp.GetLength(),NULL,0,NULL,NULL);
  char * pDebugInfo = new char[len+1]; //以字节为单位
  WideCharToMultiByte(CP_ACP,0,m_stringDisp,m_stringDisp.GetLength() + 1 ,pDebugInfo,len + 1 ,NULL,NULL);
  pDebugInfo[len+1] = 0; 
  printf("%s",pDebugInfo);
   
  输出结果为:abc??
  即所有的中文都显示成"?",我用十六进制打印了pDebugInfo数组的结果是 0x61 0x62 0x63 0x3f 0x3f
   

折腾N久了 ! (在不修改项目属性的Locale提前下,有没有办法啊?)

拜托各位大神了!!!!





------解决方案--------------------
int len = WideCharToMultiByte(936,...

试试
------解决方案--------------------
我的笨办法:
int len=str.GetLength();
TCHAR *tbuf=str.GetBuffer(len);
char *buf= new char[len+1]; 
for(int i=0;i<len;i++) 
buf[i]=char(tbuf[i]);
buf[i]='\0';
str.ReleaseBuffer();
------解决方案--------------------
探讨

网上有人说用setlocale,想问一下,这个函数加在代码哪个位置,还有,我的编译器报错,
error C3861: 'setlocale': identifier not found

setlocale( LC_ALL, "chs" )//把这行代码写到你的程序里就能解决你的问题了,不用字符串的转换

------解决方案--------------------
WideCharToMultiByte(CP_ACP,0,m_stringDisp,m_stringDisp.GetLength() ,pDebugInfo,len,NULL,NULL);
pDebugInfo[len] = 0;



  相关解决方案