unicode 的体会

来源:互联网 发布:淘宝账号在哪里查看 编辑:程序博客网 时间:2024/05/12 05:52
关于windows下使用的函数:
1:windows api 涉及到字符串参数的 ANSI版本函数,均是在函数内部转换为 unicode,然后使用unicode版本函数进行调用,来完成函数功能,所以使用windows api使用 unicode较好。

2:ansi 函数有unicode版本的函数,但是其内部是把unicode转换为 ansi c单字节,使用,故调用ansi c函数,则使用ansi 编码较好

这样做,估计是为了稳定。
unicode 部分编码:
UTF-8:
     0x00 -  0x7f           1B
     0x008f - 0x07ff      2B
     0X0800 - 0XFFFFFF      3B
                                   4B 
     节省空间,适合于网络传输

UTF-16:
     windows 标准
     2B   4B  
     中庸之道。

UTF-32:
     统一4字节,解码快,内存占用多,一般在本地使用


//code
// from  ansi to unico
void CStringTostring(CString theCString, std::string &thestring, DWORD theType)
{
     int len = WideCharToMultiByte(theType, 0, theCString, -1, NULL, 0, NULL, NULL);
     char* pCxml = new char[len + 1];
     len = WideCharToMultiByte(theType, 0, theCString, theCString.GetLength(), pCxml, len + 1, NULL, NULL);
     pCxml[len] = 0;
     std::string tString(pCxml);
     thestring = tString;
     delete[] pCxml;
    
}

void stringToCString(std::string thestring, CString &theCString, int theType))
{
     int len = MultiByteToWideChar(theType, 0, thestring.c_str(), -1, NULL, 0);
     WCHAR* pCxml = new WCHAR[len + 1];
     len = MultiByteToWideChar(theType, 0, thestring.c_str(), thestring.length(), pCxml, len);
     pCxml[len] = 0;
     CString tString(pCxml);
     theCString = tString;
     delete[] pCxml;
}
0 0
原创粉丝点击