ANSI、UTF-8、UNICODE字符串相互转化
来源:互联网 发布:mac打字法不见了 编辑:程序博客网 时间:2024/05/18 03:08
std::wstring AnsiToUnicode(const char* src)
{
if (src == NULL || src[0] == '\0')
{
return L"";
}
std::string strSrc(src);
int length = MultiByteToWideChar(CP_ACP, 0, strSrc.c_str(), -1, NULL, 0);
wchar_t *buf = new wchar_t[length + 1];
MultiByteToWideChar(CP_ACP, 0, strSrc.c_str(), -1, buf, length);
buf[length] = L'\0';
std::wstring dest = buf;
delete [] buf;
return dest;
}
std::string AnsiToUTF8(const char* src)
{
std::wstring unicode = AnsiToUnicode(src);
return UnicodeToUTF8(unicode.c_str());
}
std::string UTF8ToAnsi(const char* src)
{
std::wstring unicode = UTF8ToUnicode(src);
return UnicodeTochar(unicode.c_str());
}
std::wstring UTF8ToUnicode(const char* src)
{
if (src == NULL || src[0] == '\0')
{
return L"";
}
std::string strSrc(src);
int length = MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(), -1, NULL, 0);
wchar_t *buf = new wchar_t[length + 1];
MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(), -1, buf, length);
buf[length] = L'\0';
std::wstring dest = buf;
delete [] buf;
return dest;
}
std::string UnicodeToUTF8(const wchar_t* src)
{
if (src == NULL || src[0] == '\0')
{
return "";
}
std::wstring strSrc(src);
int length = WideCharToMultiByte(CP_UTF8, 0, strSrc.c_str(),
-1, NULL, 0, NULL, FALSE);
char *buf = new char[length + 1];
WideCharToMultiByte(CP_UTF8, 0, strSrc.c_str(), -1, buf, length, NULL, FALSE);
buf[length] = '\0';
std::string dest = buf;
delete [] buf;
return dest;
}
tstring UTF8ToNative(const char* src)
{
#ifdef UNICODE
return UTF8ToUnicode(src);
#else
return UTF8ToAnsi(src);
#endif
}
std::string NativeToUTF8(LPCTSTR src)
{
#ifdef UNICODE
return UnicodeToUTF8(src);
#else
return AnsiToUTF8(src);
#endif
}
std::wstring TCHARToUnicode(LPCTSTR src)
{
if (src == NULL || src[0] == '\0')
{
return L"";
}
#ifdef UNICODE
return src;
#else
return AnsiToUnicode(src);
#endif
}
std::string UnicodeTochar(const wchar_t*src)
{
if (!src)
return "";
int unicode_len = wcslen(src);
int charcount = ::WideCharToMultiByte(CP_ACP, 0, src, unicode_len, NULL, 0, NULL, NULL);
if (charcount == 0)
return "";
std::string mb;
mb.resize(charcount);
::WideCharToMultiByte(CP_ACP, 0, src, unicode_len, &mb[0], charcount, NULL, NULL);
return mb;
}
bool UTF8ToUnicode(std::wstring& str_out, const std::string& str_in)
{
int len = str_in.length();
int unicodeLen = MultiByteToWideChar( CP_UTF8,
0,
str_in.c_str(),
-1,
NULL,
0 );
if (unicodeLen <= 0){
return false;
}
wchar_t* pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
int ret = MultiByteToWideChar( CP_UTF8,
0,
str_in.c_str(),
-1,
(LPWSTR)pUnicode,
unicodeLen );
if (ret <= 0){
delete[] pUnicode;
return false;
}
str_out = ( wchar_t* )pUnicode;
delete[] pUnicode;
return true;
}
bool UnicodeToUTF8(std::string& str_out, const std::wstring& str_in)
{
char* pElementText;
int iTextLen;
iTextLen = WideCharToMultiByte( CP_UTF8,
0,
str_in.c_str(),
-1,
NULL,
0,
NULL,
NULL );
if (iTextLen <=0 ){
return false;
}
pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
int ret = WideCharToMultiByte( CP_UTF8,
0,
str_in.c_str(),
-1,
pElementText,
iTextLen,
NULL,
NULL );
if (ret <= 0){
delete[] pElementText;
return false;
}
str_out= pElementText;
delete[] pElementText;
return true;
}
bool UnicodeToMulti(std::string& str_out, const std::wstring& str_in)
{
char* pElementText;
int iTextLen;
iTextLen = WideCharToMultiByte( CP_ACP,
0,
str_in.c_str(),
-1,
NULL,
0,
NULL,
NULL );
if (iTextLen <=0 ){
return false;
}
pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
int ret = WideCharToMultiByte( CP_ACP,
0,
str_in.c_str(),
-1,
pElementText,
iTextLen,
NULL,
NULL );
if (ret <= 0){
delete[] pElementText;
return false;
}
str_out= pElementText;
delete[] pElementText;
return true;
}
bool MultiToUnicode(std::wstring& str_out, const std::string& str_in)
{
int len = str_in.length();
int unicodeLen = MultiByteToWideChar( CP_ACP,
0,
str_in.c_str(),
-1,
NULL,
0 );
if (unicodeLen <= 0){
return false;
}
wchar_t* pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
int ret = MultiByteToWideChar( CP_ACP,
0,
str_in.c_str(),
-1,
(LPWSTR)pUnicode,
unicodeLen );
if (ret <= 0){
delete[] pUnicode;
return false;
}
str_out = ( wchar_t* )pUnicode;
delete[] pUnicode;
return true;
}
0 0
- ANSI、UTF-8、UNICODE字符串相互转化
- ANSI,UNICODE, UTF-8 字符串之间的相互转换
- ANSI,UNICODE, UTF-8 字符串之间的相互转换
- ansi unicode utf-8相互转换
- ansi、Unicode、Utf-8之间的转化
- unicode、utf-8、ansi
- unicode、utf-8、ansi
- ANSI UNICODE UTF-8
- VC++版 Unicode ANSI UTF-8 GB2312 相互转换代码
- unicode、utf-8、ansi的故事及其相互转换
- ANSI UNICODE 字符串相互转换
- ASCII,UTF-8,Unicode字符串相互转换
- ASCII,UTF-8,Unicode字符串相互转换
- ASCII,UTF-8,Unicode字符串相互转换
- ANSI、Unicode、UTF8相互转化的函数
- ANSI和UNICODE字符的相互转化
- windows 下UNICODE 多字节和utf-8相互转化
- ANSI UNIcode UNIcodebig UTF-8
- Android动画_基本
- 【总结】UML图之用例图
- VS2008--VS2013 各种版本官方下载地址
- JAVA使用Dom4j实现字符串和xml文件相互转换
- mysql查询数据,根据条件更新到另一张表
- ANSI、UTF-8、UNICODE字符串相互转化
- 使用 advanced installer 为 winform 做自动更新
- 使用CocoaPods删除已经配置的类库和移除CocoaPods
- maven模块化管理
- hdu1115 Lifting the Stone (求多边形重心)
- 同步异步和阻塞非阻塞
- Java学习笔记---集合(二)Map
- gSOAP Multithreading
- 正则表达式