宽字符至多字符的相互转换方法

1. unicode宽字符至多字符的转换

char *w2c(char *pcstr,const wchar_t *pwstr, size_t len)
{
    int nLength=wcslen(pwstr);
   
    //获取转换后的长度
    int nBytes = WideCharToMultiByte( // Bytes required for "hello" is 5, does not include null terminator
        0, // Specify the code page used to perform the conversion
        0,         // No special flags to handle unmapped characters
        pwstr,   // Wide character string to convert
        nLength,   // The number of wide characters in that string
        NULL,      // No output buffer given, we just want to know how long it needs to be
        0,
        NULL,      // No replacement character given
        NULL );
   
    // make sure the buffer is big enough for this, making it larger if necessary
    if(nBytes>len)nBytes=len;
   
    // 通过以上得到的结果,转换unicode 字符为ascii 字符
    WideCharToMultiByte( // Writes 5 bytes "hello", does not write a null terminator after that
        0, // Specify the code page used to perform the conversion
        0,         // No special flags to handle unmapped characters
        pwstr,   // Wide character string to convert
        nLength,   // The number of wide characters in that string
        pcstr, // Put the output ASCII characters at the end of the buffer
        nBytes,                           // There is at least this much space there
        NULL,      // No replacement character given
        NULL );
   
    return pcstr ;
}

2. 把asii字符转换为unicode字符,和上面相同的原理

void c2w(wchar_t *pwstr,size_t len,const char *str)

{
 if(str)
    {
      size_t nu = strlen(str);
      size_t n =(size_t)MultiByteToWideChar(CP_ACP,0,(const char *)str,(int)nu,NULL,0);
      if(n>=len)n=len-1;
      MultiByteToWideChar(CP_ACP,0,(const char *)str,(int)nu,pwstr,(int)n);
  pwstr[n]=0;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值