宽字符至多字符的相互转换方法
2010-01-07 23:00
218 查看
1. unicode宽字符至多字符的转换
char *w2c(char *pcstr,const wchar_t *pwstr, size_t len)
{
int nLength=wcslen(pwstr);
//获取转换后的长度
int nBytes = WideCharToMultiByte( // Bytes required for "hello" is 5, does not include null terminator
0, // Specify the code page used to perform the conversion
0, // No special flags to handle unmapped characters
pwstr, // Wide character string to convert
nLength, // The number of wide characters in that string
NULL, // No output buffer given, we just want to know how long it needs to be
0,
NULL, // No replacement character given
NULL );
// make sure the buffer is big enough for this, making it larger if necessary
if(nBytes>len)nBytes=len;
// 通过以上得到的结果,转换unicode 字符为ascii 字符
WideCharToMultiByte( // Writes 5 bytes "hello", does not write a null terminator after that
0, // Specify the code page used to perform the conversion
0, // No special flags to handle unmapped characters
pwstr, // Wide character string to convert
nLength, // The number of wide characters in that string
pcstr, // Put the output ASCII characters at the end of the buffer
nBytes, // There is at least this much space there
NULL, // No replacement character given
NULL );
return pcstr ;
}
2. 把asii字符转换为unicode字符,和上面相同的原理
void c2w(wchar_t *pwstr,size_t len,const char *str)
{
if(str)
{
size_t nu = strlen(str);
size_t n =(size_t)MultiByteToWideChar(CP_ACP,0,(const char *)str,(int)nu,NULL,0);
if(n>=len)n=len-1;
MultiByteToWideChar(CP_ACP,0,(const char *)str,(int)nu,pwstr,(int)n);
pwstr
=0;
}
}
char *w2c(char *pcstr,const wchar_t *pwstr, size_t len)
{
int nLength=wcslen(pwstr);
//获取转换后的长度
int nBytes = WideCharToMultiByte( // Bytes required for "hello" is 5, does not include null terminator
0, // Specify the code page used to perform the conversion
0, // No special flags to handle unmapped characters
pwstr, // Wide character string to convert
nLength, // The number of wide characters in that string
NULL, // No output buffer given, we just want to know how long it needs to be
0,
NULL, // No replacement character given
NULL );
// make sure the buffer is big enough for this, making it larger if necessary
if(nBytes>len)nBytes=len;
// 通过以上得到的结果,转换unicode 字符为ascii 字符
WideCharToMultiByte( // Writes 5 bytes "hello", does not write a null terminator after that
0, // Specify the code page used to perform the conversion
0, // No special flags to handle unmapped characters
pwstr, // Wide character string to convert
nLength, // The number of wide characters in that string
pcstr, // Put the output ASCII characters at the end of the buffer
nBytes, // There is at least this much space there
NULL, // No replacement character given
NULL );
return pcstr ;
}
2. 把asii字符转换为unicode字符,和上面相同的原理
void c2w(wchar_t *pwstr,size_t len,const char *str)
{
if(str)
{
size_t nu = strlen(str);
size_t n =(size_t)MultiByteToWideChar(CP_ACP,0,(const char *)str,(int)nu,NULL,0);
if(n>=len)n=len-1;
MultiByteToWideChar(CP_ACP,0,(const char *)str,(int)nu,pwstr,(int)n);
pwstr
=0;
}
}
相关文章推荐
- 宽字符至多字符的相互转换方法
- 宽字符至多字符的相互转换方法
- mysql日期和字符相互转换方法
- 【Sql】mysql类似to_char()to_date()函数mysql日期和字符相互转换方法date_f
- C#实现char字符数组与字符串相互转换的方法
- mysql日期和字符相互转换方法
- json对象与javaBean,String字符创之间相互转换的方法
- Wince下GB(国标字符)和Unicode的相互转换方法
- C++宽字符与普通字符的相互转换方法
- mysql日期和字符相互转换方法
- oracle to_char()to_date()函数 mysql日期和字符相互转换方法
- mysql日期和字符相互转换方法
- ArcGIS学习记录—Arcgis中点、线、面的相互转换方法
- python实现字典(dict)和字符串(string)的相互转换方法
- java中字符数组和字符串的相互转换
- 设置节点属性;setInterval和setTimeout;转换为字符串;获取字符位置方法;检测字符串长度
- CBitmap和HBITMAP的区别及相互转换方法
- java中数组与List相互转换的方法
- Cstring与char、string、int等数据类型的相互转换方法
- mySQL转换字符编码的方法