转载地址: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字符串相互转换相关推荐

  1. python3中unicode怎么写_详解python3中ascii与Unicode使用

    这篇文章主要为大家详解python3中ascii与Unicode使用的相关资料,需要的朋友可以参考下# Auther: Aaron Fan ''' ASCII:不支持中文,1个英文占1个字节 Unic ...

  2. php 字符ascii转中文,PHP ASCII码与字符串相互转换的方法

    PHP ASCII码与字符串相互转换的方法 PHP ASCII码与字符串如何相互转换你知道吗?你对PHP ASCII码与字符串相互转换了解吗?下面是小编为大家带来的PHP ASCII码与字符串相互转换 ...

  3. C++中ASCII、unicode与Utf8之间的相互转化

    头文件:UTN.h [cpp] view plaincopy print? #pragma once #include "stdafx.h" #include "wind ...

  4. 字符和字节详解、Java中字节串和字符串相互转换

    字符.字节和编码 1. 程序中的字符与字节 字节是规定存储大小的存储单位,规定为8位一字节(8bit = 1 byte). 字符是人类的描述符号.存储在计算机时,不同的编码格式会有不同的字节组合,一般 ...

  5. php 获取ascii码,PHP实现ASCII码与字符串相互转换的方法

    class ascii { /** * 将ascii码转为字符串 * @param type $str 要解码的字符串 * @param type $prefix 前缀,默认: * @return t ...

  6. java中日期类型与字符串相互转换

    View Code 1 import java.text.SimpleDateFormat; 2 import java.util.Date; 3 import java.text.ParseExce ...

  7. C# 中对象与JSON字符串相互转换的三种方法

    JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. 关于内存对象和JSON字符串的相互转换,在实际项目中应比较广泛,经过一番搜索,找到如下 ...

  8. Labview串口通信中ASCII码和数值相互转换

    对于串口通信,labview提供了visa的写入和读取函数,网上关于如何使用有大把的介绍,但是其写入和读取都是缓冲区,所以对应的控件都是字符串,在串口通信是,自动转换成字符对应的ASCII码的十六进制 ...

  9. linux ascii转换 utf8,C++中ASCII、unicode与Utf8之间的相互转化

    一.windows下转换方法: // 当type为CP_ACP时,GBK转化为UNICODE:当type为CP_UTF8时,UTF8转化为UNICODE wchar_t* trans(const ch ...

最新文章

  1. 拍牌神器是怎样炼成的(三)---注册全局热键
  2. 数字货币 矿池 矿场 区别
  3. 制定备份策略的指导方向思考
  4. FlashDevelop flex sdk 报错的奇怪问题
  5. 如何安装 Linux 下的 Adobe Reader
  6. fortran subroutine_Fortran:派生数组与数组传递进子程序耗费时间比较
  7. EF Core 小技巧:迁移已经应用到数据库,如何进行迁移回退操作?
  8. python3调用c语言数组,使用Python中的ctypes访问数组
  9. Mangos源码分析(15):游戏对象的实现
  10. Linux网络协议指令:ifconfig/netstat(net-tools)工具 .vs. iproute2
  11. 【李宏毅机器学习】05:概率生成模型Probabilistic Generative Model
  12. Java学习笔记——JDBC之与数据库MySQL的连接以及增删改查等操作
  13. 第二次结对编程作业——毕业导师智能匹配
  14. python菜鸟教程网址是什么-Python菜鸟教程
  15. Centos下ftp的安装和配置
  16. python 3d绘图kmeans_使用Python matplotlib绘制3D多边形!
  17. 企业信息与网络通信安全 团队成员简历-叶俊
  18. fiddler--HTTP协议调试工具
  19. xp计算机限制打开u盘,winxp电脑提示U盘拒绝访问怎么修复
  20. ORACLE 体系结构详细图

热门文章

  1. apache ab压力测试学习
  2. 容器学习 之 管理Volumn(十七)
  3. Leecode 9. 回文数
  4. 【通俗易懂】C语言中,for循环中i++与++i的区别
  5. (*长期更新)软考网络工程师学习笔记——Section 5 数据链路层
  6. Python中os库的使用
  7. 传统公司部署OpenStack(t版)简易介绍(八)——Dashboard模块部署
  8. 华为ensp小实验(路由下发+Easy IP+单臂路由+OSPF+Rip)
  9. m6000路由器产品介绍与基本操作_交换机与路由器的区别及光模块搭配方案_光模块吧...
  10. 浏览器 刷新页面后回到顶部_JavaScript仿知乎回到顶部功能