utf8_to_utf16

时间:2022-08-29 15:33:48
17down voteaccepted

Here's some code. Only lightly tested and there's probably a few improvements. Call this function to convert a UTF-8 string to a UTF-16 wstring. If it thinks the input string is not UTF-8 then it will throw an exception, otherwise it returns the equivalent UTF-16 wstring.

std::wstring utf8_to_utf16(const std::string& utf8)
{
std::vector<unsigned long> unicode;
size_t i = 0;
while (i < utf8.size())
{
unsigned long uni;
size_t todo;
bool error = false;
unsigned char ch = utf8[i++];
if (ch <= 0x7F)
{
uni = ch;
todo = 0;
}
else if (ch <= 0xBF)
{
throw std::logic_error("not a UTF-8 string");
}
else if (ch <= 0xDF)
{
uni = ch&0x1F;
todo = 1;
}
else if (ch <= 0xEF)
{
uni = ch&0x0F;
todo = 2;
}
else if (ch <= 0xF7)
{
uni = ch&0x07;
todo = 3;
}
else
{
throw std::logic_error("not a UTF-8 string");
}
for (size_t j = 0; j < todo; ++j)
{
if (i == utf8.size())
throw std::logic_error("not a UTF-8 string");
unsigned char ch = utf8[i++];
if (ch < 0x80 || ch > 0xBF)
throw std::logic_error("not a UTF-8 string");
uni <<= 6;
uni += ch & 0x3F;
}
if (uni >= 0xD800 && uni <= 0xDFFF)
throw std::logic_error("not a UTF-8 string");
if (uni > 0x10FFFF)
throw std::logic_error("not a UTF-8 string");
unicode.push_back(uni);
}
std::wstring utf16;
for (size_t i = 0; i < unicode.size(); ++i)
{
unsigned long uni = unicode[i];
if (uni <= 0xFFFF)
{
utf16 += (wchar_t)uni;
}
else
{
uni -= 0x10000;
utf16 += (wchar_t)((uni >> 10) + 0xD800);
utf16 += (wchar_t)((uni & 0x3FF) + 0xDC00);
}
}
return utf16;
}

http://*.com/questions/7153935/how-to-convert-utf-8-stdstring-to-utf-16-stdwstring

#pragma once
#include <string> #ifdef tstring
#error "\"tstring\" Macro has been defined."
#else
#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif
#endif class EncodingConverter
{
public:
static int AnsiStrToWideStr(std::string& strSrc, std::wstring& strDest)
{
int nLen = strSrc.length() + ;
int nRet = ; nLen *= sizeof(wchar_t); wchar_t* pszW = new wchar_t[nLen];
memset(pszW, , nLen); nRet = MultiByteToWideChar(CP_ACP, , strSrc.c_str(), -, pszW, nLen); strDest = pszW;
delete[] pszW; return nRet;
}; static int WideStrToAnsiStr(std::wstring& strSrc, std::string& strDest)
{
int nLen = strSrc.length() + ;
int nRet = ; nLen *= sizeof(wchar_t); char* pszA = new char[nLen];
memset(pszA, , nLen); nRet = WideCharToMultiByte(CP_ACP, , strSrc.c_str(), -, pszA, nLen, NULL, NULL); strDest = pszA;
delete[] pszA; return nRet;
}; static int AnsiStrToTStr(std::string& strSrc, std::tstring& strDest)
{
int nRet = ; #ifdef _UNICODE
nRet = AnsiStrToWideStr(strSrc, strDest);
#else
strDest = strSrc;
nRet = strDest.length();
#endif return nRet;
}; static int TStrToAnsiStr(std::tstring& strSrc, std::string& strDest)
{
int nRet = ; #ifdef _UNICODE
nRet = WideStrToAnsiStr(strSrc, strDest);
#else
strDest = strSrc;
nRet = strDest.length();
#endif return nRet;
}; static int WideStrToTStr(std::wstring& strSrc, std::tstring& strDest)
{
int nRet = ; #ifdef _UNICODE
strDest = strSrc;
nRet = strDest.length();
#else
nRet = WideStrToAnsiStr(strSrc, strDest);
#endif return nRet;
}; static int TStrToWideStr(std::tstring& strSrc, std::wstring& strDest)
{
int nRet = ; #ifdef _UNICODE
strDest = strSrc;
nRet = strDest.length();
#else
nRet = AnsiStrToWideStr(strSrc, strDest);
#endif return nRet;
}; static std::string ToAnsiString(const wchar_t* lpStr)
{
std::wstring wide_string = lpStr;
std::string ansi_string; WideStrToAnsiStr(wide_string, ansi_string);
return ansi_string;
}; static std::string ToAnsiString(const char* lpStr)
{
return std::string(lpStr);
}; static std::wstring ToWideString(const wchar_t* lpStr)
{
return std::wstring(lpStr);
}; static std::wstring ToWideString(const char* lpStr)
{
std::string ansi_string = lpStr;
std::wstring wide_string; AnsiStrToWideStr(ansi_string, wide_string);
return wide_string;
}; static std::tstring ToTString(const char* lpStr)
{
#ifdef _UNICODE
return ToWideString(lpStr);
#else
return ToAnsiString(lpStr);
#endif
}; static std::tstring ToTString(const wchar_t* lpStr)
{
#ifdef _UNICODE
return ToWideString(lpStr);
#else
return ToAnsiString(lpStr);
#endif
}; static int WideStrToUtf8Str(std::wstring& strSrc, std::string& strDest)
{
int nRet = ;
int nLen = ; nLen = WideCharToMultiByte(CP_UTF8, , strSrc.c_str(), -, NULL, , NULL, NULL); char * lpUtf8Str = new char[nLen+];
memset(lpUtf8Str, , nLen);
nRet = WideCharToMultiByte(CP_UTF8, , strSrc.c_str(), -, lpUtf8Str, nLen, NULL, NULL);
strDest = lpUtf8Str;
delete[] lpUtf8Str; return nRet;
}; static int AnsiStrToUtf8Str(std::string& strSrc, std::string& strDest)
{
int nRet = ;
std::wstring wide_string; nRet = AnsiStrToWideStr(strSrc, wide_string);
nRet = WideStrToUtf8Str(wide_string, strDest); return nRet;
}; static int Utf8StrToWideStr(const std::string& strSrc, std::wstring& strDest)
{
int nRet = ;
int nLen = ; nLen = MultiByteToWideChar(CP_UTF8, , strSrc.c_str(), -, NULL, ); wchar_t* lpWideStr = new wchar_t[nLen];
memset(lpWideStr, , nLen*sizeof(lpWideStr[]));
nRet = MultiByteToWideChar(CP_UTF8, , strSrc.c_str(), -, lpWideStr, nLen);
strDest = lpWideStr;
delete[] lpWideStr; return nRet;
}; static int Utf8StrToAnsiStr(const std::string& strSrc, std::string& strDest)
{
int nRet = ;
std::wstring wide_string; nRet = Utf8StrToWideStr(strSrc, wide_string);
nRet = WideStrToAnsiStr(wide_string, strDest); return nRet;
}; static int Utf8StrToTStr(const std::string& strSrc, std::tstring& strDest)
{
#ifdef UNICODE
return Utf8StrToWideStr(strSrc, strDest);
#else
return Utf8StrToAnsiStr(strSrc, strDest);
#endif
}; static std::string ToUtf8String(const std::string& str)
{
std::string ansi_string = str;
std::string utf8_string; AnsiStrToUtf8Str(ansi_string, utf8_string);
return utf8_string;
}; static std::string ToUtf8String(const std::wstring& str)
{
std::wstring wide_string = str;
std::string utf8_string; WideStrToUtf8Str(wide_string, utf8_string);
return utf8_string;
};
};

https://github.com/yaocoder/utility/blob/master/src/common/EncodingConverter.h

utf8_to_utf16的更多相关文章

  1. boost&colon;&colon;xml——基本操作以及中文乱码解决方案 (续)

    本博文主要想说明以下两点: 1.对于上一篇的<boost::xml——基本操作以及中文乱码解决方案>解释,这篇博文基本解决了正确输入输出中英文问题,但是好像还没有解决修改中文出现乱码的问题 ...

  2. C&plus;&plus; MFC std&colon;&colon;string转为 std&colon;&colon;wstring

    std::string转为 std::wstring std::wstring UTF8_To_UTF16(const std::string& source) { unsigned long ...

随机推荐

  1. ajax 异步加载显示等待效果

    css: #loading { width:170px; height:25px; border:3px solid #C3DAF9; position:absolute; top:300px; le ...

  2. 如何用js刷新aspxgridviw

    //写在js中 ASPxGridView1.Refresh();

  3. JS 键值对

    function Map() { this.keys = new Array(); this.data = new Array(); //添加键值对 this.set = function (key, ...

  4. eclispe中安装hibernate插件

    用eclispe玩ee的朋友,写配置文件的时候没有提示非常苦恼,而配置dtd文件还是没有得到解决,最后试了试安装插件解决了问题 地址:http://download.jboss.org/jbossto ...

  5. CentOS查看和修改PATH环境变量的方法 profile

    https://blog.csdn.net/dongheli/article/details/83987092

  6. 谈谈Windows Wow64

    欢迎转载,转载请注明出处:http://www.cnblogs.com/lanrenxinxin/p/4977488.html 本文是<深入理解Windows操作系统 (第六版) >关于6 ...

  7. 大数据开发实战:Hadoop数据仓库开发实战

    1.Hadoop数据仓库架构设计 如上图. ODS(Operation Data Store)层:ODS层通常也被称为准备区(Staging area),它们是后续数据仓库层(即基于Kimball维度 ...

  8. 如何用cacti监控windwos

    1:模版下载地址 https://github.com/mrlesmithjr/cacti resource \ snmp_queries 的文件放到cacti服务器对应的目录下 导入模版文件(在te ...

  9. java getenv getProperties区别

    网上很多使用的是getProperties.说获得系统变量,但是其实不正确.getProperties中所谓的"system properties"其实是指"java s ...

  10. 《深入理解Linux内核》阅读笔记 --- Chapter 2 Memory Addressing

    1.logical address = segment identifier (16bits) + offset (32bits) segment selector其实就是GDT或者LDT的索引,其中 ...

相关文章