C/C++中ASCII与Unicode字符串相互转换
生活随笔
收集整理的這篇文章主要介紹了
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 100int main(void) {size_t i;char *pMBBuffer = (char *)malloc(BUFFER_SIZE);wchar_t *pWCBuffer = L"Hello, world.";printf("Convert wide-character string:\n");//Conversionwcstombs_s(&i, pMBBuffer, (size_t)BUFFER_SIZE, pWCBuffer, (size_t)BUFFER_SIZE);//Outputprintf(" Characters converted: %u\n", i);printf(" Multibyte character: %s\n\n", pMBBuffer);//Free multibyte character bufferif (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 convertint requiredSize;unsigned char *pmbnull = NULL;unsigned char *pmbhello = NULL;char *localeInfo;wchar_t *pwchello = L"\x3042\x3043"; //2 Hiragana characterswchar_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_sprintf("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_sif (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); //C4996if (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); }?
總結
以上是生活随笔為你收集整理的C/C++中ASCII与Unicode字符串相互转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不依赖任何系统API,用c语言实现gbk
- 下一篇: cocos2d学习路线