MultiByteToWideChar和WideCharToMultiByte用法

MultiByteToWideChar和WideCharToMultiByte

       在软件的使用过程中,经常碰到乱码的情况,那么乱码的原因就是字符集的不同,也就是说同一个数字代表不同意思。在Windows里,目前主要有ANSI和UNICODE的方式。如果在UNICODE的方式里直接显示ANSI的字符串是出现乱码的,同样在ANSI函数里也不能显示UNICODE的字符串,而是要进行相互转换才能显示正确的字符串。在NT以后的操作系统里, Windows底层函数已经全部改为UNICODE的方式,如果还是使用ANSI的话,要比UNICODE函数慢一些, 系统底层会从ANSI的方式转换为UNICODE方式,然后再显示出来。从ANSI转换为UNICODE的字符串,就可以使用函数MultiByteToWideChar来实现。

 函数MultiByteToWideChar声明如下:

1
2
3
4
5
6
7
8
9
10
WINBASEAPI
int
WINAPI
MultiByteToWideChar(
    __in UINT     CodePage,
    __in DWORD    dwFlags,
    __in LPCSTR   lpMultiByteStr,
    __in int      cbMultiByte,
    __out_ecount_opt(cchWideChar) LPWSTR lpWideCharStr,
    __in int      cchWideChar);

  CodePage是代码表。//常用的代码页由CP_ACP和CP_UTF8两个。CP_ACP实了ANSI与Unicode之间的转换。CP_UTF8代码页实现UTF-8与Unicode之间的转换。

  dwFlags是转换标志。

  lpMultiByteStr是输入ANSI字符串。

  cbMultiByte是输入ANSI的字符串长度。//为-1时,表示该字符串是以NULL结尾的字符串,其长度会自动计入字符串长度之内,

  lpWideCharStr是输出UNICODE字符串。

  cchWideChar是输出UNICODE字符串的缓冲区大小。//如果该值为0,函数返回值为需要的宽字符字节大小



下面是代码实现:
ANSI <--> Unicode <--> UTF8


wchar_t * ANSIToUnicode( const char* str )
{
        int    textlen ;
        wchar_t * result;
        textlen = MultiByteToWideChar( CP_ACP, 0, str,-1,    NULL,0 );   
        result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t));   
        memset(result,0,(textlen+1)*sizeof(wchar_t));   
        MultiByteToWideChar(CP_ACP, 0,str,-1,(LPWSTR)result,textlen );   
        return    result;   
}

/***********************************************************************/
void AnsiToUnicode(WCHAR *dst, const char *str)
{
    if( str == NULL ) 
    {
        return;
    }
    int len = (int)strlen(str);
    int nLen = MultiByteToWideChar(CP_ACP, 0,(char *)str, -1, NULL, NULL);
    MultiByteToWideChar(CP_ACP, 0,(char *)str, len, dst, nLen);
    dst[nLen-1] = 0;
}

/**********************************************************************/

char * UnicodeToANSI( const wchar_t *str )
{
        char * result;
        int textlen;
        // wide char to multi char
        textlen = WideCharToMultiByte( CP_ACP,    0,    str,    -1,    NULL, 0, NULL, NULL );
        result =(char *)malloc((textlen+1)*sizeof(char));
        memset( result, 0, sizeof(char) * ( textlen + 1 ) );
        WideCharToMultiByte( CP_ACP, 0, str, -1, result, textlen, NULL, NULL );
        return result;
}

wchar_t * UTF8ToUnicode( const char* str )
{
        int    textlen ;
        wchar_t * result;
        textlen = MultiByteToWideChar( CP_UTF8, 0, str,-1,    NULL,0 );   
        result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t));   
        memset(result,0,(textlen+1)*sizeof(wchar_t));   
        MultiByteToWideChar(CP_UTF8, 0,str,-1,(LPWSTR)result,textlen );   
        return    result;   
}

char * UnicodeToUTF8( const wchar_t *str )
{
        char * result;
        int textlen;
        // wide char to multi char
        textlen = WideCharToMultiByte( CP_UTF8,    0,    str,    -1,    NULL, 0, NULL, NULL );
        result =(char *)malloc((textlen+1)*sizeof(char));
        memset(result, 0, sizeof(char) * ( textlen + 1 ) );
        WideCharToMultiByte( CP_UTF8, 0, str, -1, result, textlen, NULL, NULL );
        return result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值