2

Say you have "methods"

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;
}

What is the proper way to get that int correctly to WCHAR?

CS.
  • 1,845
  • 1
  • 19
  • 38

4 Answers4

2

Use mbstowcs (multibyte string to wide character string) :

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 uses the current locale, so that should match the encoding of your input.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • @yanbellavance: Sounds weird. Write a small program that demonstrates the problem, and you can then ask a new question. But you probably will find out the problem yourself when you try to isolate the problem. – MSalters Apr 15 '14 at 19:31
1
convert char <= => wchar
in windows:
MultiByteToWideChar
WideCharToMultiByte

in linux:
mbsrtowcs
wcsrtombs
chlaws
  • 30
  • 4
0
#include<tchar.h>

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

    return 0;

}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
0

After reading When should static_cast, dynamic_cast and reinterpret_cast be used?, I realize it was my lack of knowledge about casting that triggered me to ask this question.

Community
  • 1
  • 1
CS.
  • 1,845
  • 1
  • 19
  • 38