ASCII,UTF-8,Unicode字符串相互转换
#include<string>
#include<windows.h>
#include<vector>
using namespace std; //utf8 转 Unicode
std::wstring Utf82Unicode(const std::string& utf8string)
{
int widesize = ::MultiByteToWideChar(CP_UTF8, , utf8string.c_str(), -, NULL, );
if (widesize == ERROR_NO_UNICODE_TRANSLATION)
{
throw std::exception("Invalid UTF-8 sequence.");
}
if (widesize == )
{
throw std::exception("Error in conversion.");
} std::vector<wchar_t> resultstring(widesize); int convresult = ::MultiByteToWideChar(CP_UTF8, , utf8string.c_str(), -, &resultstring[], widesize); if (convresult != widesize)
{
throw std::exception("La falla!");
} return std::wstring(&resultstring[]);
} //unicode 转为 ascii
std::string WideByte2Acsi(std::wstring& wstrcode)
{
int asciisize = ::WideCharToMultiByte(CP_OEMCP, , wstrcode.c_str(), -, NULL, , NULL, NULL);
if (asciisize == ERROR_NO_UNICODE_TRANSLATION)
{
throw std::exception("Invalid UTF-8 sequence.");
}
if (asciisize == )
{
throw std::exception("Error in conversion.");
}
std::vector<char> resultstring(asciisize);
int convresult =::WideCharToMultiByte(CP_OEMCP, , wstrcode.c_str(), -, &resultstring[], asciisize, NULL, NULL); if (convresult != asciisize)
{
throw std::exception("La falla!");
} return std::string(&resultstring[]);
} //utf-8 转 ascii
std::string UTF_82ASCII(std::string& strUtf8Code)
{
std::string strRet("");
//先把 utf8 转为 unicode
std::wstring wstr = Utf82Unicode(strUtf8Code);
//最后把 unicode 转为 ascii
strRet = WideByte2Acsi(wstr);
return strRet;
} /////////////////////////////////////////////////////////////////////// //ascii 转 Unicode
std::wstring Acsi2WideByte(std::string& strascii)
{
int widesize = MultiByteToWideChar (CP_ACP, , (char*)strascii.c_str(), -, NULL, );
if (widesize == ERROR_NO_UNICODE_TRANSLATION)
{
throw std::exception("Invalid UTF-8 sequence.");
}
if (widesize == )
{
throw std::exception("Error in conversion.");
}
std::vector<wchar_t> resultstring(widesize);
int convresult = MultiByteToWideChar (CP_ACP, , (char*)strascii.c_str(), -, &resultstring[], widesize); if (convresult != widesize)
{
throw std::exception("La falla!");
} return std::wstring(&resultstring[]);
} //Unicode 转 Utf8
std::string Unicode2Utf8(const std::wstring& widestring)
{
int utf8size = ::WideCharToMultiByte(CP_UTF8, , widestring.c_str(), -, NULL, , NULL, NULL);
if (utf8size == )
{
throw std::exception("Error in conversion.");
} std::vector<char> resultstring(utf8size); int convresult = ::WideCharToMultiByte(CP_UTF8, , widestring.c_str(), -, &resultstring[], utf8size, NULL, NULL); if (convresult != utf8size)
{
throw std::exception("La falla!");
} return std::string(&resultstring[]);
} //ascii 转 Utf8
std::string ASCII2UTF_8(std::string& strAsciiCode)
{
std::string strRet("");
//先把 ascii 转为 unicode
std::wstring wstr = Acsi2WideByte(strAsciiCode);
//最后把 unicode 转为 utf8
strRet = Unicode2Utf8(wstr);
return strRet;
}
转载于:https://blog.csdn.net/Don211/article/details/51769515
ASCII,UTF-8,Unicode字符串相互转换的更多相关文章
- Java中如何输出对勾,ASCII编码与字符串相互转换
Java中如何输出对勾? 最简单的方法是,从那个地方拷贝一个对勾的字符,然后System.out.println("√"); 但是心里总会担心,万一机器不认这个字符该怎么办?(可能 ...
- 第48篇 字符编码探密--ASCII,UTF8,GBK,Unicode
原文地址:http://blog.laofu.online/2017/08/22/encode-string/ ASCII 的由来 在计算机的“原始社会”,有人想把日常的使用的语言使用计算机来表示, ...
- 利用浏览器做好数字进制和ASCII码及Unicode教与学
浏览器是现在个人计算机的标配,一般来说一个PC至少安装一种以上的浏览器.主流网页浏览器有Google Chrome.Internet Explorer.Microsoft Edge.Mozilla F ...
- Python中Unicode字符串
Python中Unicode字符串 字符串还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte ...
- UNICODE字符串与多字节字符串的转换
相互转换的两个函数的声明: 1. 多字节字符串与宽字符串的转换 int MultiByteToWideChar( UINT CodePage, // code page,一般设为 CP_ACP DWO ...
- 关于python中的unicode字符串的使用
基于python2.7中的字符串: unicode-->编码encode('utf-8')-->写入文件 读出文件-->解码decode('utf-8')-->unicode ...
- Python Cookbook(第3版)中文版:15.14 传递Unicode字符串给C函数库
15.14 传递Unicode字符串给C函数库¶ 问题¶ 你要写一个扩展模块,需要将一个Python字符串传递给C的某个库函数,但是这个函数不知道该怎么处理Unicode. 解决方案¶ 这里我们需要考 ...
- 中文字符串和UTF-8编码字符串相互转换
中文字符串和UTF-8编码字符串相互转换 //UTF字符转换 var UTFTranslate = { Change: function(pValue) { ) { ).replace(/(%u)(\ ...
- Python 入门(二)Unicode字符串
Unicode字符串 字符串还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节 (byte),所以,一 ...
随机推荐
- windows环境下mosquitto环境搭建与mqtt测试
https://blog.csdn.net/pgpanda/article/details/51800865 工作需求,自己在windows下搭建了一个mosquitto环境测试mqtt 话不多说,直 ...
- vue 自己编写向左滑动的动画 仿transition
vue 模板代码: <div class="content-wrap clearfix" :class="{slideIn: showIn, slideOut: s ...
- jqGrid怎样冻结指定的列
第一步设置colModel 列内的 frozen: true: 第二步设置表格初始时宽度 shrinkToFit: false, 第三步设置 $gridTable.jqGrid('setFroze ...
- IFC构件位置信息—ObjectPlacement
在IFC标准中,采用相对坐标系对构件定位.如柱(IfcColumn)的定位信息(局部坐标系及参考坐标系)由ObjectPlacement描述.ObjectPlacement由两部分组成: (1)Pla ...
- CharUtil
package com.opslab.util; import java.io.UnsupportedEncodingException; /** * Various character and ch ...
- 搭建iscsi存储系统(一)
(1).DAS.SAN.NAS三种存储方式 参考:https://blog.csdn.net/qq_23348071/article/details/73963407 DAS全称Direct-Atta ...
- WebService学习总结(三)——使用JDK开发WebService(转)
一.WebService的开发手段 使用Java开发WebService时可以使用以下两种开发手段 1. 使用JDK开发(1.6及以上版本) 2.使用CXF框架开发(工作中) 二.使用JDK开发Web ...
- Python - Django - ORM F查询和Q查询
models.py: from django.db import models # 出版社 class Publisher(models.Model): id = models.AutoField(p ...
- centos7上安装mysql8(上)
1.删除系统现存的mysql rpm -pa | grep mysql 2.卸载mysql组件 yum remove mysql-xxx-xxx- 3.删除mysql的配置文件,卸载不会自动删除配置文 ...
- python:datetime.datetime is not JSON serializable 报错问题解决
问题: 项目使用django开发,返回的数据中有时间字段,当json.dumps()时提示:datetime.datetime is not JSON serializable 解决: import ...