none
convert BYTE to _TCHAR

Answers

  • what we are looking for is  to convert an array of unsigned char to an array of char


    A BYTE (unsigned char) and a char are the same size.
    Just copy it or use a typecast.

    BYTE Barray[10] = "123456789";
    char Carray[10];
    memcpy(Carray, Barray, sizeof Carray);

    >We get data as BYTE which we need to convert to wchar_t.

    BYTE Barray[10] = "123456789";
    wchar_t Warray[10];
    mbstowcs(Warray, (char*)Barray, _countof(Warray));

    - Wayne

    Thursday, September 06, 2012 8:14 AM
  • mbstowcs(Warray, (char*)Barray, _countof(Warray));


    Note that this will stop if a nul character is found
    in the source. i.e. - It does a *string* conversion.

    If the data is not nul-terminated (a C string), you can
    iterate over all characters of the array and use mbtowc()
    instead of mbstowcs():

    BYTE Barray2[5] = {'1', '2', '3', '4', '5'};
    wchar_t Warray2[5] = {0};
    for(int n=0; n<_countof(Warray2); ++n)
      {
      mbtowc(&Warray2[n], (char*)&Barray2[n], 1);
      }

    - Wayne

    Thursday, September 06, 2012 8:41 AM

All replies

  • Need to convert BYTE array to _TCHAR array (or string or wstring)

    BYTE is an alias for an unsigned char.

    Windows Data Types
    http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751%28v=vs.85%29.aspx


    _TCHAR is an alias for char if Unicode is not enabled
    for the project, and wchar_t if it is.

    Generic-Text Mappings in Tchar.h
    http://msdn.microsoft.com/en-us/library/c426s321%28v=vs.100%29.aspx

    So what you're asking for is to convert an array of
    unsigned char to an array of char or wchar_t. Which
    is it you want to do? Is your project Unicode-enabled
    or not?

    - Wayne


    • Edited by WayneAKing Thursday, September 06, 2012 7:32 AM spelling
    Thursday, September 06, 2012 7:10 AM
  • Need to convert BYTE array to ... string or wstring


    BYTE Barray[10] = "123456789";

    string str1((char*)Barray);
    string str2(reinterpret_cast<char*>(Barray));

    - Wayne

    Thursday, September 06, 2012 7:20 AM
  • Our project is unicode enabled. 

    what we are looking for is  to convert an array of unsigned char to an array of char or wchar_t. We get data as BYTE which we need to convert to wchar_t. 

    Regards

    Sai Kalyan


    saikalyan

    Thursday, September 06, 2012 7:51 AM
  • what we are looking for is  to convert an array of unsigned char to an array of char


    A BYTE (unsigned char) and a char are the same size.
    Just copy it or use a typecast.

    BYTE Barray[10] = "123456789";
    char Carray[10];
    memcpy(Carray, Barray, sizeof Carray);

    >We get data as BYTE which we need to convert to wchar_t.

    BYTE Barray[10] = "123456789";
    wchar_t Warray[10];
    mbstowcs(Warray, (char*)Barray, _countof(Warray));

    - Wayne

    Thursday, September 06, 2012 8:14 AM
  • mbstowcs(Warray, (char*)Barray, _countof(Warray));


    Note that this will stop if a nul character is found
    in the source. i.e. - It does a *string* conversion.

    If the data is not nul-terminated (a C string), you can
    iterate over all characters of the array and use mbtowc()
    instead of mbstowcs():

    BYTE Barray2[5] = {'1', '2', '3', '4', '5'};
    wchar_t Warray2[5] = {0};
    for(int n=0; n<_countof(Warray2); ++n)
      {
      mbtowc(&Warray2[n], (char*)&Barray2[n], 1);
      }

    - Wayne

    Thursday, September 06, 2012 8:41 AM