汉字to unicode && mbstowcs编辑

来源:互联网 发布:通达信敢死队资金源码 编辑:程序博客网 时间:2024/05/16 09:37

1
2
3
4
5
6
7
8
#include <stdio.h>
#include <wchar.h>
 
int main()
{
        wchar_t h[]=L"你好";
        printf("%X%X\n", h[0], h[1]);
}


执行
$ ./a.out 

4F60597D


收藏
26
5

mbstowcs编辑

本词条缺少概述信息栏名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧!
mbstowcs - 把多字符转换成宽字符
头文件:
#include <stdlib.h> [1] 
函数原型:
size_t mbstowcs(wchar_t *dest, const char *src, size_t n);
说明:
如dest 非NULL,则mbstowcs() 函数把多字符src转换成宽字符dest,最多转换n字节。
返回值:
转换成功,返回的是内容个数(不包括非0字符),不成功返回(size_t)(-1)。
例如:以下程序的功能
#include <stdio.h>
  #include <locale.h>
  #include <string.h>
  #include <stdlib.h>
  int main(void)
  {
  char buf[256]={"你好啊"};
  setlocale(LC_ALL,"zh_CN.UTF-8");
  wchar_t ar[256]={'\0'};
  int read=mbstowcs(ar,buf,strlen(buf));
  printf("%d\n",strlen(buf)); //输出为:9 [字节] UF-8编码下一个汉字占三个字节3*3=9
  printf("%d\n",read); //输出为:3 [个数] “你好啊”三个子字个数
  }
注意:
mbstowcs函数的行为受当前locale的决定。
std::string curLocale = setlocale(LC_ALL, NULL);
setlocale(LC_ALL, "chs");

0 0
原创粉丝点击