#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. Windows使用telnet验证服务端口是否通

    使用telnet指令时,Windows需要开启Telnet服务. telnet不通的情况: a.端口对应的服务没启动,或者启动了服务端口不是对应的测试端口. b.端口受限不能访问. 以下内容转自:ht ...

  2. Flutter -------- 加载本地图片资源和网络图片

    在Flutter加载本地图片资源 在Flutter项目目录下创建文件夹 images ,在文件夹中添加几张图片 指定资源 pubspec.yaml文件中 version: 1.0.0+1 enviro ...

  3. Centos7 安装 weblogic12.2.1.0.0

    下载地址:地址:http://www.oracle.com/technetwork/middleware/weblogic/downloads/wls-main-097127.html 下载最新的we ...

  4. [原][工具][global mapper]查看图元属性(查看shp文件属性值)

    常用的shp文件需要查看其内部字段 目前常用的有三种方法: 1.使用excel打开dbf文件,直接查看shp数据库文本文件 2.使用global mapper查看shp图元,然后通过内部工具查看“图元 ...

  5. Django 引用{% url "name"%} 避免链接硬编码

    前提条件:为每个url指定name且name值要唯一.比如: 项目中的url.py文件: urlpatterns = patterns('', url(r'^$',TemplateView.as_vi ...

  6. Python - Django - request 对象

    request.method: 获取请求的方法,例如 GET.POST 等 views.py: from django.shortcuts import render, HttpResponse # ...

  7. Win10安装Golang

    首先去这个网站下载Golang的安装包:https://studygolang.com/dl 因为我的系统是Win10专业版64位,所以我选择了对应的Windows的安装包进行下载: 下载好安装包之后 ...

  8. 了解 Selenium 定位方式

    ※元素定位的重要性:在于查找元素 And 执行元素 定位元素的三种方法 1.定位单个元素:在定位单个元素时,selenium-webdriver 提示了如下一些方法对元素进行定位.在这些定位方式中,优 ...

  9. 【C/C++开发】值得学习的C语言开源项目

    值得学习的C语言开源项目 - 1. Webbench Webbench是一个在Linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的 ...

  10. 解决jQuery版本冲突

    解决jquery版本冲突问题 <!-- 引入1.6.4版的jq --><script src="http://ajax.googleapis.com/ajax/libs/j ...