多字节与宽字节相互转换的方法
2007-12-26 15:12
274 查看
也就是WideCharToMultiByte和MultiByteToWideChar函数的使用方法。
多字节向宽字节转换:
wstring converToWideChar( const string& str )
{
int len = 0;
len = str.length();
int unicodeLen = ::MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,NULL,0);
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
::MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,(LPWSTR)pUnicode,unicodeLen);
wstring rt;
rt = ( wchar_t* )pUnicode;
delete pUnicode;
return rt;
}
宽字节向多字节转换:
string converToMultiChar( const wstring& str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_ACP,
0,
str.c_str(),
-1,
NULL,
0,
NULL,
NULL );
pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
::WideCharToMultiByte( CP_ACP,
0,
str.c_str(),
-1,
pElementText,
iTextLen,
NULL,
NULL );
string strText;
strText = pElementText;
delete[] pElementText;
return strText;
}
多字节向宽字节转换:
wstring converToWideChar( const string& str )
{
int len = 0;
len = str.length();
int unicodeLen = ::MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,NULL,0);
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
::MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,(LPWSTR)pUnicode,unicodeLen);
wstring rt;
rt = ( wchar_t* )pUnicode;
delete pUnicode;
return rt;
}
宽字节向多字节转换:
string converToMultiChar( const wstring& str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_ACP,
0,
str.c_str(),
-1,
NULL,
0,
NULL,
NULL );
pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
::WideCharToMultiByte( CP_ACP,
0,
str.c_str(),
-1,
pElementText,
iTextLen,
NULL,
NULL );
string strText;
strText = pElementText;
delete[] pElementText;
return strText;
}
相关文章推荐
- 十六进制数与字节数组相互转换方法
- 多字节与宽字节相互转换的方法
- 在C#中将任意数值类型数据与字节数组相互转换的一个方法
- 字节码问题--wchar和char的区别以及wchar和char之间的相互转换字符编码转换等方法及函数介绍
- 字节码问题--wchar和char的区别以及wchar和char之间的相互转换字符编码转换等方法及函数介绍
- 图片与字节数组相互转换的方法
- C#华氏温度和摄氏温度相互转换的方法
- java中数组与List相互转换的方法
- Android中 Bitmap和Drawable相互转换的方法
- C# 字符串与字节数组相互转换
- C#字节数组与字符串相互转换代码案例
- 整型变量(int)与字节数组(byte[])的相互转换
- JS对象与json字符串相互转换实现方法示例
- Google Gson的使用方法,实现Json结构的相互转换
- Numpy中矩阵matrix读取一列的方法及数组和矩阵的相互转换
- Android中 Bitmap和Drawable相互转换的方法
- C# 字符串与字节数组相互转换
- Bitmap和Drawable相互转换方法
- java 汉字与UTF-8十六进制编码 间相互转换方法
- JAVA中整型和字符串相互转换的方法