zoukankan      html  css  js  c++  java
  • UNICODE字符串与多字节字符串的转换

    相互转换的两个函数的声明:

    1. 多字节字符串与宽字符串的转换
    int MultiByteToWideChar(
        UINT CodePage,         // code page,一般设为 CP_ACP
        DWORD dwFlags,         // character-type options,一般为设0
        LPCSTR lpMultiByteStr, // string to map,指向一个多字节字符串
        int cbMultiByte,       // number of bytes in string,多字节字符串的长度(字节数,当以0结尾的时候,也可以设为-1)
        LPWSTR lpWideCharStr,  // wide-character buffer,存放转换后的宽字符串缓冲区
        int cchWideChar        // size of buffer,宽字符串缓冲区的最大长度(字符数)
      );
    2. 宽字符串与多字节字符串的转换
    int WideCharToMultiByte(
        UINT CodePage,            // code page,一般设为 CP_ACP
        DWORD dwFlags,            // performance and mapping flags,一般为设0
        LPCWSTR lpWideCharStr,    // wide-character string,指向一个宽字符串
        int cchWideChar,          // number of chars in string,宽字符串的长度(字符数)
        LPSTR lpMultiByteStr,     // buffer for new string,存放转换后的多字节字符串缓冲区
        int cbMultiByte,          // size of buffer,多字节字符串缓冲区的最大长度(字节数)
        LPCSTR lpDefaultChar,     // default for unmappable chars,转换失败的字符所显示的字符串,一般设为NULL
        LPBOOL lpUsedDefaultChar  // set when default char used,如果有字符转换失败,则为TRUE,一般设为NULL
      );
    3. 用法
    #include <STDLIB.H>
    #include <LOCALE.H>
    #include <Windows.h>
    
    int main(int argc, char* argv[])
    {
        setlocale(LC_ALL,"");    // 否则无法输出中文
    
        char    ansiStr[] = "这是一个ANSI字符串";
        printf( "ansiStr: %s
    ", ansiStr );
    
        wchar_t wideStr[] = L"这是一个UNICODE字符串";
        wprintf( L"wideStr: %s
    ", wideStr );
    
    
        // 转换ANSI字符串到UNICODE字符串
        int len = MultiByteToWideChar(CP_ACP, 0, ansiStr, -1, NULL, 0);  // 先取得转换后的UNICODE字符串所需的长度
        wchar_t* buf1 = (wchar_t*)calloc(len, sizeof(wchar_t));         // 分配缓冲区
        MultiByteToWideChar(CP_ACP, 0, ansiStr, -1, buf1, len);    // 开始转换
        wprintf(L"转换后的UNICODE字符串: %s
    ", buf1);                 // 输出转换后的字符串
        free(buf1);                                                                             // 释放缓冲区
    
        // 转换UNICODE字符串到ANSI字符串
        len = WideCharToMultiByte(CP_ACP, 0, wideStr, -1, NULL, 0, NULL, NULL);  // 先取得转换后的ANSI字符串所需的长度
        char* buf2 = (char*)calloc(len, sizeof(char));                                             // 分配缓冲区
        WideCharToMultiByte(CP_ACP, 0, wideStr, -1, buf2, len, NULL, NULL);   // 开始转换
        printf("转换后的ANSI字符串: %s
    ", buf2);                                                 // 输出转换后的字符串
        free(buf2);                                                                                                 // 释放缓冲区
    
        return 0;
    }
     
  • 相关阅读:
    买房的贷款时间是否是越长越好?https://www.zhihu.com/question/20842791
    asp.net cookie and session
    leelazero and google colab
    download file by python in google colab
    physical processor, core, logical processor
    通过powershell操作eventlog
    openxml in sql server
    get the page name from url
    How to Execute Page_Load() in Page's Base Class?
    Difference between HttpContext.Request and Request
  • 原文地址:https://www.cnblogs.com/MrYuan/p/5171249.html
Copyright © 2011-2022 走看看