我試圖實現函數,它將char *轉換爲wchar_t *。但問題是,wprintf顯示不同的結果。我究竟做錯了什麼?char * to wchar_t *函數實現
wchar_t *toWchar(char *data)
{
if(!data)
{
return NULL;
}
int size = strlen(data);
if(!size)
{
return NULL;
}
char *temp = (char *)malloc(size * 2);
if(!temp)
{
return NULL;
}
int j = 0;
for(int i = 0; i < size; i++)
{
temp[j++] = data[i];
temp[j++] = '\0';
}
return (wchar_t *)temp;
}
編輯: 主要功能:
int main()
{
wchar_t *temp = toWchar("hello, world!");
if(temp)
wprintf("%ls\n", temp);
return 0;
}
你使用的是什麼編碼?什麼編碼字符,什麼編碼wchar_t?你的代碼可以工作,如果字符是ASCII。並且請注意,您需要添加一個空終止符。 –
嗚嗚,這麼多問題!你能給我一些提示怎麼辦?我只是編程的初學者,我不知道我使用的是哪種編碼。如果這些信息對您有幫助,我在Linux機器上使用普通英語作爲語言環境。 – user2399415
你需要弄清楚你使用的是什麼編碼。你爲什麼要轉換爲wchar_t?你爲什麼不使用庫函數? –