美文网首页
Windows数据类型转换

Windows数据类型转换

作者: Virtualer | 来源:发表于2019-08-28 09:52 被阅读0次

Windows程序开发中常用的字符数据类型为TCHAR或者CString,此二者本质可以是CHARCStringA, 也可以是WCHARCStringW。如果定义了UNICODE那么就是后者宽字符的形式。
界面和任务交互时会需要将char或者string转换成以上两种。在与web通信时需要将GB2312转化为utf-8

1. char和string与TCHAR和CString的转换

- char与WCHAR
BOOL C2W(const char* str, wchar_t* wstr)
{
      int len = MultiByteToWideChar(CP_OEMCP, 0, str, -1, wstr, 0);
      return len == MultiByteToWideChar(CP_OEMCP, 0, str, -1, wstr, len);
}

BOOL W2C(const wchar_t* wstr, char* str)
{
      int len = WideCharToMultiByte(CP_OEMCP, 0, wstr, -1, str, 0, 0, 0);
      return len == WideCharToMultiByte(CP_OEMCP, 0, wstr, -1, str, len, 0, 0);
}
- CStringA与CStringW
//  
 // CStringA转CStringW  
 //  
 CStringW CStrA2CStrW(const CStringA &cstrSrcA)  
 {  
     int len = MultiByteToWideChar(CP_ACP, 0, LPCSTR(cstrSrcA), -1, NULL, 0);  
     wchar_t *wstr = new wchar_t[len];  
     memset(wstr, 0, len*sizeof(wchar_t));  
     MultiByteToWideChar(CP_ACP, 0, LPCSTR(cstrSrcA), -1, wstr, len);  
     CStringW cstrDestW = wstr;  
     delete[] wstr;  
   
     return cstrDestW;  
 }  
   
 //  
 // CStringW转CStringA  
 //  
 CStringA CStrW2CStrA(const CStringW &cstrSrcW)  
 {  
     int len = WideCharToMultiByte(CP_ACP, 0, LPCWSTR(cstrSrcW), -1, NULL, 0, NULL, NULL);  
     char *str = new char[len];  
     memset(str, 0, len);  
     WideCharToMultiByte(CP_ACP, 0, LPCWSTR(cstrSrcW), -1, str, len, NULL, NULL);  
     CStringA cstrDestA = str;  
     delete[] str;  
   
     return cstrDestA;  
 }
- GB2312与UTF-8
//GB2312到UTF-8的转换
char *G2U(const char *gb2312) {
    int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
    wchar_t *wstr = new wchar_t[len + 1];
    memset(wstr, 0, len + 1);

    MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
    len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
    
    char *str = new char[len + 1];
    memset(str, 0, len + 1);
    WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
    
    if (wstr) delete[] wstr;
    return str;
}

char* U2G(const char* utf8)
{
    int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
    wchar_t* wstr = new wchar_t[len+1];
    memset(wstr, 0, len+1);
    
    MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
    len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
    
    char* str = new char[len+1];
    memset(str, 0, len+1);
    WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
    
    if(wstr) delete[] wstr;
    return str;
}

从以上的代码不难看出,其实本质都是通过MultiByteToWideCharWideCharToMultiByte的使用达到宽窄字符之间的转换。

这里说两句闲话,VC++把字符串搞得这么复杂真的是让人蛋疼,特别是与web通信需要转换GB2312和UTF-8,着实让我彻彻底底恶心了一把。看看稍微新一点的语言,Python、Swift等等都是直接支持utf-8,根本不需要这么复杂的转换。当然,这也与VC++语言的产生时代有关。当时的环境下能支持UNICODE已经是神级操作了,更遑论消息循环和拖动控件这种逆天机制。微软真是一个伟大的公司。MFC以现在的眼光来看确实不美观,VC++也太过繁琐。我想随着时间的推移,VC++和MFC短时间内应该不会被淘汰,但是层出不穷的新语言让编程更简单,应该终会被C#取代。

相关文章

  • Windows数据类型转换

    Windows程序开发中常用的字符数据类型为TCHAR或者CString,此二者本质可以是CHAR和CString...

  • JAVA基础第四天

    JAVA数据类型---布尔类型; 数据类型转换自动数据类型转换 强制数据类型转换

  • JavaScript基础03- 数据类型转换

    数据类型转换 所谓的数据类型转换,就是将一种数据类型转换为另一种数据类型,所以,数据类型转换包括,转换为字符串类型...

  • JS里的数据类型转换

    在js中,数据类型转换分为显式数据类型转换和隐式数据类型转换。 1, 显式数据类型转换 a:转数字: 1)Numb...

  • javascript-强制类型转换和运算符

    强制类型转换 将其它数据类型转换为String 将其它数据类型转换为Number 将其它数据类型转换为Boolea...

  • Python自学笔记——3.基础数据类型转换

    基础数据类型转换 数据类型转换的形式自动类型转换强制类型转换 可变数据类型: 列表, 字典, 集合不可变数...

  • 3-数据类型转换

    数据类型转换 将数据有当前类型变化为其他类型的操作就是数据类型转换。 数据类型转换分类: 数据类型转换一共分为2类...

  • Java基础语法_数据类型转换

    数据类型转换 当数据类型不一样时,将会发生数据类型转换。 数据类型转换——自动转换(隐式) 特点:代码不需要进行特...

  • 其他类型转换成字符串类型

    数据类型强制转换 字符串相关数据类型转换

  • JavaScript字符串转换为其他类型(五)

    目录: 为什么要数据类型转换? 数据类型怎么转换? 一、为什么要数据类型转换? 上节课咱们讲到了数据类型好比人的职...

网友评论

      本文标题:Windows数据类型转换

      本文链接:https://www.haomeiwen.com/subject/rqauectx.html