#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字符串相互转换的更多相关文章

  1. Java中如何输出对勾,ASCII编码与字符串相互转换

    Java中如何输出对勾? 最简单的方法是,从那个地方拷贝一个对勾的字符,然后System.out.println("√"); 但是心里总会担心,万一机器不认这个字符该怎么办?(可能 ...

  2. 第48篇 字符编码探密--ASCII,UTF8,GBK,Unicode

    原文地址:http://blog.laofu.online/2017/08/22/encode-string/ ASCII 的由来 在计算机的“原始社会”,有人想把日常的使用的语言使用计算机来表示, ...

  3. 利用浏览器做好数字进制和ASCII码及Unicode教与学

    浏览器是现在个人计算机的标配,一般来说一个PC至少安装一种以上的浏览器.主流网页浏览器有Google Chrome.Internet Explorer.Microsoft Edge.Mozilla F ...

  4. Python中Unicode字符串

    Python中Unicode字符串 字符串还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte ...

  5. UNICODE字符串与多字节字符串的转换

    相互转换的两个函数的声明: 1. 多字节字符串与宽字符串的转换 int MultiByteToWideChar( UINT CodePage, // code page,一般设为 CP_ACP DWO ...

  6. 关于python中的unicode字符串的使用

    基于python2.7中的字符串: unicode-->编码encode('utf-8')-->写入文件 读出文件-->解码decode('utf-8')-->unicode ...

  7. Python Cookbook(第3版)中文版:15.14 传递Unicode字符串给C函数库

    15.14 传递Unicode字符串给C函数库¶ 问题¶ 你要写一个扩展模块,需要将一个Python字符串传递给C的某个库函数,但是这个函数不知道该怎么处理Unicode. 解决方案¶ 这里我们需要考 ...

  8. 中文字符串和UTF-8编码字符串相互转换

    中文字符串和UTF-8编码字符串相互转换 //UTF字符转换 var UTFTranslate = { Change: function(pValue) { ) { ).replace(/(%u)(\ ...

  9. Python 入门(二)Unicode字符串

    Unicode字符串 字符串还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节 (byte),所以,一 ...

随机推荐

  1. layui 提交验证以及field作用

    设置值时只需data.field.name(input中name属性)=1;即可赋值data.field.index_desc = layedit.getContent(index_desc); $. ...

  2. AndoridSQLite数据库开发基础教程(5)

    AndoridSQLite数据库开发基础教程(5) 创建SQLite数据库 使用SQLiteManager创建数据库的操作步骤如下: (1)双击SQLiteManager工具,弹出SQliteMana ...

  3. hive分区表新增字段,已有分区显示为null

    如果在hive的分区表新增非分区字段,那么原有的分区的数据即使重新运行也会显示为null. 必须先删除该分区,再重新跑数据.

  4. Vue路由编程式导航以及hash模式

    import Vue from 'vue'; import App from './App.vue'; //引入公共的scss 注意:创建项目的时候必须用scss import './assets/c ...

  5. Spark累加器(Accumulator)

    一.累加器简介 在Spark中如果想在Task计算的时候统计某些事件的数量,使用filter/reduce也可以,但是使用累加器是一种更方便的方式,累加器一个比较经典的应用场景是用来在Spark St ...

  6. IDEA Git 修改后的文件无法Commit

    转自:https://blog.csdn.net/moneyshi/article/details/81298861 因对IDEA使用不熟,在使用和配置GIT的时候,可能哪里配置错误,导致我一直无法使 ...

  7. 如何:创建返回 UI 的外接程序

    https://msdn.microsoft.com/zh-cn/library/bb909849(v=vs.100).aspx

  8. 123457123457#0#-----com.tym.PuzzleGame28--前拼后广--日常pt-tym

    com.tym.PuzzleGame28--前拼后广--日常pt-tym

  9. 【ROC+AUC】

    http://m.elecfans.com/article/736801.html https://blog.csdn.net/xyz1584172808/article/details/818392 ...

  10. python扫描器-sqlalchemy入库操作

    学习 [Python]Flask系列-数据库笔记 实践 #!/usr/bin/env python # -*- coding: utf-8 -*- from sqlalchemy import cre ...