日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

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

發布時間:2024/1/23 c/c++ 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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字符串相互转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。