首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

:string和Unicode字符怎么相互转换?

2012-02-07 
求救:string和Unicode字符如何相互转换???如题。 [解决办法]size_t mbstowcs(wchar_t *wcstr,const char *m

求救:string和Unicode字符如何相互转换???
如题。


[解决办法]
size_t mbstowcs(
wchar_t *wcstr,
const char *mbstr,
size_t count
);
将多字节字符串转换为宽字符串.

size_t wcstombs(
char *mbstr,
const wchar_t *wcstr,
size_t count
);
将宽字符串转换为多字节字符串

Win32提供API实现这样的功能:
int WideCharToMultiByte(
UINT CodePage, // code page
DWORD dwFlags, // performance and mapping flags
LPCWSTR lpWideCharStr, // wide-character string
int cchWideChar, // number of chars in string.
LPSTR lpMultiByteStr, // buffer for new string
int cbMultiByte, // size of buffer
LPCSTR lpDefaultChar, // default for unmappable chars
LPBOOL lpUsedDefaultChar // set when default char used
);

int MultiByteToWideChar(
UINT CodePage, // code page
DWORD dwFlags, // character-type options
LPCSTR lpMultiByteStr, // string to map
int cbMultiByte, // number of bytes in string
LPWSTR lpWideCharStr, // wide-character buffer
int cchWideChar // size of buffer
);

另外,一般情况下Unicode字符不回显示在控制台窗口上,既wcout < < L "字符不 " < < endl; "字符不 "不回正确显示.可以使用MessageBox显示MessageBox(NULL, L "字符 ", L "控制台窗口 ", 0);

====================================================================================================
// crt_mbstowcs.c
// compile with: /W1
// illustrates the behavior of the mbstowcs function

#include <stdlib.h>
#include <stdio.h>
#include <locale.h>

int main( void )
{
size_t size;
int nChar = 2; // number of characters to convert
int requiredSize;

unsigned char *pmbnull = NULL;
unsigned char *pmbhello = NULL;
char* localeInfo;

wchar_t *pwchello = L "\x3042\x3043 "; // 2 Hiragana characters
wchar_t *pwc;

/* Enable the Japanese locale and codepage */
localeInfo = setlocale(LC_ALL, "Japanese_Japan.932 ");
printf( "Locale information set to %s\n ", localeInfo);

printf( "Convert to multibyte string:\n " );

requiredSize = wcstombs( NULL, pwchello, 0); // C4996
// Note: wcstombs is deprecated; consider using wcstombs_s
printf( " Required Size: %d\n ", requiredSize);

/* Add one to leave room for the null terminator. */
pmbhello = (unsigned char *)malloc( requiredSize + 1);
if (! pmbhello)
{
printf( "Memory allocation failure.\n ");
return 1;
}
size = wcstombs( pmbhello, pwchello, requiredSize + 1); // C4996
// Note: wcstombs is deprecated; consider using wcstombs_s
if (size == (size_t) (-1))
{
printf( "Couldn 't convert string. Code page 932 may "
" not be available.\n ");
return 1;
}
printf( " Number of bytes written to multibyte string: %u\n ",
(unsigned int) size );
printf( " Hex values of the " );
printf( " multibyte characters: %#.2x %#.2x %#.2x %#.2x\n ",
pmbhello[0], pmbhello[1], pmbhello[2], pmbhello[3] );
printf( " Codepage 932 uses 0x81 to 0x9f as lead bytes.\n\n ");

printf( "Convert back to wide-character string:\n " );

/* Assume we don 't know the length of the multibyte string.
Get the required size in characters, and allocate enough space. */



requiredSize = mbstowcs(NULL, pmbhello, 0); // C4996
/* Add one to leave room for the NULL terminator */
pwc = (wchar_t *)malloc( (requiredSize + 1) * sizeof( wchar_t ));
if (! pwc)
{
printf( "Memory allocation failure.\n ");
return 1;
}
size = mbstowcs( pwc, pmbhello, requiredSize + 1); // C4996
if (size == (size_t) (-1))
{
printf( "Couldn 't convert string--invalid multibyte character.\n ");
}
printf( " Characters converted: %u\n ", (unsigned int)size );
printf( " Hex value of first 2 " );
printf( " wide characters: %#.4x %#.4x\n\n ", pwc[0], pwc[1] );
free(pwc);
free(pmbhello);
}

============
Sample Output

Locale information set to Japanese_Japan.932
Convert to multibyte string:
Required Size: 4
Number of bytes written to multibyte string: 4
Hex values of the multibyte characters: 0x82 0xa0 0x82 0xa1
Codepage 932 uses 0x81 to 0x9f as lead bytes.

Convert back to wide-character string:
Characters converted: 2
Hex value of first 2 wide characters: 0x3042 0x3043



[解决办法]
知道UNICODE_STRING 什么结构就知道怎么转了

typedef struct _UNICODE_STRING
{
USHORT Length; //长度
USHORT MaxLength;//最大长度
PWSTR Buffer; //字符缓冲区
} UNICODE_STRING,*PUNICODE_STRING;


就是要把一个PWSTR (宽字符串)转成(char*)

热点排行
Bad Request.