sqlite中文乱码问题原因分析及解决

随心笔谈2年前发布 admin
193 0 0

文章摘要

本文介绍了多个C++函数,用于实现不同编码之间的转换操作。这些函数主要涉及以下内容: 1. **UTF-8到Unicode**:通过`Utf82Unicode`函数实现,用于将UTF-8字符串转换为Unicode。 2. **Unicode到ASCII**:通过`WideByte2Acsi`函数实现,用于将Unicode字符串转换为ASCII字符串。 3. **UTF-8到ASCII**:通过`UTF_82ASCII`函数实现,首先将UTF-8字符串转换为Unicode,再将Unicode转换为ASCII字符串。 4. **ASCII到Unicode**:通过`Acsi2WideByte`函数实现,用于将ASCII字符串转换为Unicode。 5. **UTF-8到Unicode(Unicode到UTF-8)**:通过`Unicode2Utf8`函数实现,用于将Unicode字符串转换为UTF-8字符串。 6. **ASCII到UTF-8**:通过`ASCII2UTF_8`函数实现,首先将ASCII字符串转换为Unicode,再将Unicode转换为UTF-8字符串。 这些函数在转换过程中均包含错误处理机制,用于检测并处理无效的编码输入。总体而言,文章展示了如何在C++中实现多种进制之间的转换操作。

//UTF-8转Unicode

std::wstring Utf82Unicode(const std::string& utf8string)

{

int widesize=::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0);

if (widesize==ERROR_NO_UNICODE_TRANSLATION)

{

throw std::exception(“Invalid UTF-8 sequence.”);

}

if (widesize==0)

{

throw std::exception(“Error in conversion.”);

}

std::vector resultstring(widesize);

int convresult=::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize);

if (convresult !=widesize)

{

throw std::exception(“La falla!”);

}

return std::wstring(&resultstring[0]);

}

//unicode 转为 ascii

string WideByte2Acsi(wstring& wstrcode)

{

int asciisize=::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, NULL, 0, NULL, NULL);

if (asciisize==ERROR_NO_UNICODE_TRANSLATION)

{

throw std::exception(“Invalid UTF-8 sequence.”);

}

if (asciisize==0)

{

throw std::exception(“Error in conversion.”);

}

std::vector resultstring(asciisize);

int convresult=::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0], asciisize, NULL, NULL);

if (convresult !=asciisize)

{

throw std::exception(“La falla!”);

}

return std::string(&resultstring[0]);

}

//utf-8 转 ascii

string UTF_82ASCII(string& strUtf8Code)

{

string strRet(“”);

//先把 utf8 转为 unicode

wstring wstr=Utf82Unicode(strUtf8Code);

//最后把 unicode 转为 ascii

strRet=WideByte2Acsi(wstr);

return strRet;

}

///////////////////////////////////////////////////////////////////////

//ascii 转 Unicode

wstring Acsi2WideByte(string& strascii)

{

int widesize=MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);

if (widesize==ERROR_NO_UNICODE_TRANSLATION)

{

throw std::exception(“Invalid UTF-8 sequence.”);

}

if (widesize==0)

{

throw std::exception(“Error in conversion.”);

}

std::vector resultstring(widesize);

int convresult=MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);

if (convresult !=widesize)

{

throw std::exception(“La falla!”);

}

return std::wstring(&resultstring[0]);

}

//Unicode 转 Utf8

std::string Unicode2Utf8(const std::wstring& widestring)

{

int utf8size=::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);

if (utf8size==0)

{

throw std::exception(“Error in conversion.”);

}

std::vector resultstring(utf8size);

int convresult=::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);

if (convresult !=utf8size)

{

throw std::exception(“La falla!”);

}

return std::string(&resultstring[0]);

}

//ascii 转 Utf8

string ASCII2UTF_8(string& strAsciiCode)

{

string strRet(“”);

//先把 ascii 转为 unicode

wstring wstr=Acsi2WideByte(strAsciiCode);

//最后把 unicode 转为 utf8

strRet=Unicode2Utf8(wstr);

return strRet;

}

© 版权声明

相关文章