将int转换为wchar_t的正确方法是什么?

时间:2012-08-21 12:48:38

标签: c++ windows type-conversion wchar

说你有“方法”

int Read(...)
{
    unsigned char Byte = 0;
    if(!ReadFile(.., &byte, 1,...))
        return -1;

    return Byte;
}


int ReadBlock(LPWSTR buffer, int cchBuffer ...)
{
    int c = 0;
    int cnt = 0;
    do
    {
        if( (c=Read(...)) != -1 )
            buffer[num++] = c; // Here.
    } while( num < ccBuffer );
    return cnt;
}

将这个int正确地传递给WCHAR的正确方法是什么?

4 个答案:

答案 0 :(得分:1)

使用mbstowcs(多字节字符串到宽字符串):

int ReadBlock(LPWSTR buffer, int cchBuffer ...)
{
    int c = 0;
    std::vector<char> narrow;
    while((c=Read(...)) != -1 )
       narrow.push_back(c);
    }
    narrow.push_back(0);
    mbstowcs(buffer, &narrow.front(), cchBuffer);
}

mbstowcs使用当前的区域设置,因此应与输入的编码匹配。

答案 1 :(得分:1)

convert char <= => wchar
in windows:
MultiByteToWideChar
WideCharToMultiByte

in linux:
mbsrtowcs
wcsrtombs

答案 2 :(得分:0)

在阅读When should static_cast, dynamic_cast and reinterpret_cast be used?之后,我意识到我缺乏关于施法的知识,这引发了我提出这个问题。

答案 3 :(得分:-1)

#include<tchar.h>

int main()
{
    int integer = 0;
    wchar_t wideCharacter = (wchar_t)integer;

    return 0;

}