BOOLEAN WideToAnsi(wchar_t* in_str, char** out_str)
{
int NeedBytes = WideCharToMultiByte(CP_ACP, 0, in_str, (int)wcslen(in_str), NULL, 0, NULL, NULL);
if (0 == NeedBytes)
{
return FALSE;
}
//多分配一个字节用于0结尾
char* Buffer = malloc(NeedBytes+1);
if (!Buffer)
{
return FALSE;
}
ZeroMemory(Buffer, NeedBytes+1);
int TurnedByets = WideCharToMultiByte(CP_ACP, 0, in_str, (int)wcslen(in_str), Buffer, NeedBytes, NULL, NULL);
if (0 == TurnedByets || TurnedByets!= NeedBytes)
{
free(Buffer);
Buffer = NULL;
return FALSE;
}
*out_str = Buffer;
return TRUE;
}
BOOLEAN AnsiToWide(char* int_str, wchar_t** out_str)
{
int NeedBytes = MultiByteToWideChar(CP_ACP, 0, int_str, -1, NULL, 0);
if (0 == NeedBytes)
{
return FALSE;
}
//多分配2字节用于0结尾
PWCHAR Buffer = malloc(NeedBytes*2);
if (!Buffer)
{
return FALSE;
}
ZeroMemory(Buffer, NeedBytes*2);
int TurnedByets = MultiByteToWideChar(CP_ACP, 0, int_str,(int)strlen(int_str)+1, Buffer, NeedBytes);
if (0 == TurnedByets || TurnedByets != NeedBytes)
{
free(Buffer);
Buffer = NULL;
return FALSE;
}
*out_str = Buffer;
return TRUE;
}
多字节和unicode的转换
最新推荐文章于 2022-12-09 11:24:03 发布