C/C++中ASCII与Unicode字符串相互转换

转载地址:https://blog.csdn.net/wbq2018/article/details/8806431

1、ASCII to Unicode

函数: wcstombs(VC6)、wcstombs_s

实例:

//crt_wcstombs_s.c
//This example converts a wide character
//string to a multibyte character string.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define BUFFER_SIZE 100

int main(void) 
{
    size_t i;
    char *pMBBuffer = (char *)malloc(BUFFER_SIZE);
    wchar_t *pWCBuffer = L"Hello, world.";

    printf("Convert wide-character string:\n");

    //Conversion
    wcstombs_s(&i, pMBBuffer, (size_t)BUFFER_SIZE, pWCBuffer, (size_t)BUFFER_SIZE);

    //Output
    printf(" Characters converted: %u\n", i);
    printf(" Multibyte character: %s\n\n", pMBBuffer);

    //Free multibyte character buffer
    if (pMBBuffer) {
        free(pMBBuffer);
    }
}

    

2、Unicode to ASCII

函数: mbstowcs

//crt_mbstowcs.c
//compile with: /W3
//illustrates the behavior of the mbstowcs function

#include <stdlib.h>
#include <stdio.h>
#include <locale.h>

int main(void) 
{
    size_t size;
    int nChar = 2; //number of characters to convert
    int requiredSize;

    unsigned char *pmbnull = NULL;
    unsigned char *pmbhello = NULL;
    char *localeInfo;

    wchar_t *pwchello = L"\x3042\x3043"; //2 Hiragana characters
    wchar_t *pwc;

    /*Enable the Japanese locale and codepage */
    localeInfo = setlocale(LC_ALL, "Japanese_Japan.932");
    printf("Locale information set to %s\n", localeInfo);

    printf("Convert to multibyte string:\n");

    requiredSize = wcstombs(NULL, pwchello, 0);  //C4966
    //Note: wcstombs is deprecated; consider using wcstombs_s
    printf("Required Size: %d\n", requiredSize);

    /*Add one to leave room for the null terminator.*/
    pmbhello = (unsigned char *)malloc(requiredSize + 1);
    if (!pmbhello) {
        printf("Memory allocation failure.\n");
        return 1;
    }
    size = wcstombs(pmbhello, pwchello, requiredSize + 1); //C4996
    //Note: wcstombs is deprecated; consider using wcstombs_s
    if (size == (size_t)(-1)) {
        printf("Couldn't convert string. Code page 932 may"
                " not be available.\n");
        return 1;
    }
    printf("Number of bytes written to multibyte string: %u\n", (unsigned int)size);
    printf("Hex values of the ");
    printf("multibyte characters: %#.2x %#.2x %#.2x %#.2x\n",
            pmbhello[0], pmbhello[1], pmbhello[2], pmbhello[3]);
    printf(" Codepage 932 uses 0x81 to 0x9f as lead bytes.\n\n");

    printf("Convert back to wide-character string:\n");

    /*Assume we don't know the length of the multibyte string.
      Get the required size in characters, and allocate enough space.*/

    requiredSize = mbstowcs(NULL, pmbhello, 0); //C4996
    /*Add one to leave room for the NULL terminator */
    pwc = (wchar_t *)malloc((requiredSize + 1) * sizeof(wchar_t));
    if (!pwc) {
        printf("Memory allocation failure.\n");
        return 1;
    }
    size = mbstowcs(pwc, pmbhello, requiredSize+1); //C4996
    if (size == (size_t)(-1)) {
        printf("Couldn't convert string--invalid multibyte character.\n");
    }
    printf("Characters converted: %u\n", (unsigned int)size);
    printf("Hex value of first 2");
    printf("wide characters: %#.4x %#.4x\n\n", pwc[0], pwc[1]);
    free(pwc);
    free(pmbhello);
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值