zoukankan      html  css  js  c++  java
  • UTF8 ANSI 相互转换的函数

     1 #include <winnls.h>
     2 #include <malloc.h>
     3 LPCSTR AnsiToUtf8(LPCSTR Ansi)
     4 {
     5   int WLength = MultiByteToWideChar(CP_ACP, 0, Ansi, -1, NULL, 0);
     6   LPWSTR pszW = (LPWSTR) _alloca((WLength+1) * sizeof(WCHAR));
     7   MultiByteToWideChar(CP_ACP, 0, Ansi, -1, pszW, WLength);
     8   
     9   int ALength = WideCharToMultiByte(CP_UTF8, 0, pszW, -1, NULL, 0, NULL, NULL);
    10   LPSTR pszA = (LPSTR)_alloca( ALength + 1);
    11   WideCharToMultiByte(CP_UTF8, 0, pszW, -1, pszA, ALength, NULL, NULL);  
    12   pszA[ALength] = 0;
    13   
    14   return pszA;
    15 }
    16 
    17 LPCSTR WcharToUtf8(LPCWSTR szUnicode)
    18 {
    19   int ALength = WideCharToMultiByte(CP_UTF8, 0, szUnicode, -1, NULL, 0, NULL, NULL);
    20   LPSTR pszA = (LPSTR)_alloca( ALength + 1);
    21   WideCharToMultiByte(CP_UTF8, 0, szUnicode, -1, pszA, ALength, NULL, NULL);  
    22   pszA[ALength] = 0;
    23   
    24   return pszA;
    25 }
    26 
    27 LPCSTR Utf8toAnsi( LPCSTR utf8 )
    28 {  
    29   int WLength = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, NULL );
    30   LPWSTR pszW = (LPWSTR) _alloca( (WLength + 1) *sizeof(WCHAR) ) ;    
    31   MultiByteToWideChar(CP_UTF8, 0, utf8, -1, pszW, WLength );
    32   pszW[WLength] = 0;
    33   
    34   int ALength = WideCharToMultiByte(CP_ACP, 0, pszW, -1, NULL, 0, NULL, NULL);
    35   LPSTR pszA = ( LPSTR ) _alloca ( ALength + 1 );
    36   WideCharToMultiByte(CP_ACP, 0, pszW, -1, pszA, ALength, NULL, NULL);
    37   pszA[ALength] = 0;
    38   
    39   return pszA;
    40 }
    41 
    42 LPCWSTR Utf8toWchar( LPCSTR utf8 )
    43 {
    44   int WLength = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, NULL );
    45   LPWSTR pszW = (LPWSTR) _alloca( (WLength + 1) *sizeof(WCHAR) ) ;    
    46   MultiByteToWideChar(CP_UTF8, 0, utf8, -1, pszW, WLength );
    47   pszW[WLength] = 0;
    48   return pszW;
    49 }
     1 //测试代码
     2 
     3   TCHAR szBuf[1204] = { "我是中国人" };
     4   TRACE("%s/n", szBuf);
     5   strcpy( szBuf, AnsiToUtf8( szBuf ));
     6   TRACE("%s/n", szBuf);
     7   strcpy( szBuf, Utf8toAnsi(szBuf));
     8   TRACE("%s/n", szBuf);
     9 
    10   strcpy( szBuf, "浣犳槸鎴戠殑濂芥湅鍙嬪悧"); 
    11   TRACE("%s/n", szBuf);
    12   strcpy( szBuf, Utf8toAnsi( szBuf ) );
    13   TRACE("%s/n",  szBuf );
    14   strcpy( szBuf, AnsiToUtf8( szBuf ) );
    15   TRACE("%s/n",  szBuf );

    调试输出

    我是中国人 (CE D2 CA C7 D6 D0 B9 FA C8 CB 00)

    鎴戞槸涓浗浜?  (E6 88 91 E6 98 AF E4 B8 AD E5 9B BD E4 BA BA 00)

    我是中国人 (CE D2 CA C7 D6 D0 B9 FA C8 CB 00)

    浣犳槸鎴戠殑濂芥湅鍙嬪悧 (E4 BD A0 E6 98 AF E6 88 91 E7 9A 84 E5 A5 BD E6 9C 8B E5 8F 8B E5 90 97 00)

    你是我的好朋友吗 (C4 E3 CA C7 CE D2 B5 C4 BA C3 C5 F3 D3 D1 C2 F0 00 )

    浣犳槸鎴戠殑濂芥湅鍙嬪悧 (E4 BD A0 E6 98 AF E6 88 91 E7 9A 84 E5 A5 BD E6 9C 8B E5 8F 8B E5 90 97 00)

  • 相关阅读:
    复杂系统架构设计<1>
    DevExpress Applications<3>
    DevExpress Skins<2>
    DevExpress MVVM<1>
    Unity容器<1>
    spring cloud微服务快速教程之(九) Spring Cloud Alibaba--sentinel-限流、熔断降级
    spring cloud微服务快速教程之(八) Spring Cloud Alibaba--nacos(二)、配置中心
    spring cloud微服务快速教程之(七) Spring Cloud Alibaba--nacos(一)、服务注册发现
    小白的springboot之路(十五)、mybatis的PageHelper分页插件使用
    spring cloud微服务快速教程之(六) 应用监控 spring boot admin
  • 原文地址:https://www.cnblogs.com/MaxWoods/p/3131478.html
Copyright © 2011-2022 走看看