char pCh[4]={98,99,100,0};
MultiByteToWideChar(UTF7,pCh);
 转换后得到字符 str;现在我想知道 str 由几个字节构成 上述结果应该为4才正确
WideCharToMultiByte(UTF7,NULL,str,-1,NULL,0,NULL,FALSE) 返回值 作为结果
这种方法好像有时候对 少数情况下不对  不知道这么做方法对吗?

解决方案 »

  1.   

    MultiByteToWideChar(UTF7,pCh);
     转换后得到字符 str;
    这时字节数是8,一个字符占两个字节WideCharToMultiByte(UTF7,NULL,str,-1,NULL,0,NULL,FALSE) 返回值 作为结果
    这时返回的数据是四个字节
      

  2.   

    WideCharToMultiByte(UTF7,NULL,str,-1,NULL,0,NULL,FALSE) 返回值 作为结果
    但很多时候不是四个字节 当然简单的情况我都测过  有些复杂的情况(比如有好多字符的时候)返回的结果好像不对 
      

  3.   

    MultiByteToWideChar 返回多少个字符数的两倍就是字节数
      

  4.   

    主要我是想得到char pCh[4]={98,99,100,0}; 中 pch的个数  而不是转换出来之后 str 的字节数 这个简单 个数*2 就行了
      

  5.   

    Return Values
    If the function succeeds, and cbMultiByte is nonzero, the return value is the number of bytes written to the buffer pointed to by lpMultiByteStr. The number includes the byte for the null terminator.If the function succeeds, and cbMultiByte is zero, the return value is the required size, in bytes, for a buffer that can receive the translated string. If the function fails, the return value is zero. To get extended error information, call GetLastError. GetLastError may return one of the following error codes:
      

  6.   

    pch中的个元素个数:
    sizeof(pCh)/sizeof(char)
      

  7.   

    ATL的宏USES_CONVERSION
    W2A
    字符串以后\0结尾的话,strlen函数还是可以用的
      

  8.   

    不是 sizeof(pCh)/sizeof(char) 这么简单
    我这个程序情况比较复杂。。
    先查找 char pCh[4]={98,99,100,0} 的某个关键字   再得到关键字 对应 在pCh中的位置我先 pch 转换成 cstring  得到在 cstring  中的位置后  在取关键字左面的所有字符 通过 MultiByteToWideChar  得到的位置 就认为是 关键字 在pch中对应的字符 但这样做有时候不对
      

  9.   

     #3楼 得分:0回复于:2012-09-06 09:33:49MultiByteToWideChar 返回多少个字符数的两倍就是字节数 
     
      

  10.   

    2个function()是对的.
    但len = 3.
      wchar_len = 6.
      

  11.   

    上面各位大神,我在做MSComm串口通信,很模糊,望大家指导。