Results 1 to 6 of 6

Thread: Convert BYTE to char or wchar_t or TCHAR

  1. #1

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Question Convert BYTE to char or wchar_t or TCHAR

    Hi, everyone
    I've a stupid question here and can someone tell me how to convert from BYTE to char/wchar_t/TCHAR.

    regards,
    Chris.C

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It depends what your BYTE array represents. If it's a normal ANSI character string, then just cast it to char* . If it's a Unicode string, cast to wchar_t*. TCHAR is a special case typedef that resolves to char when compiling for ANSI, and wchar_t when compiling for Unicode.

    If you want to actually convert between ANSI and Unicode you need the MultiByteToWideChar function:
    Code:
    #include <tchar.h>
    
    #ifdef UNICODE
    typedef wstring tstring;
    #else
    typedef string tstring;
    #endif
    
    string WideToANSI(wstring sIn) {
    	int iReq = WideCharToMultiByte(CP_ACP, 0, sIn.c_str(), -1, NULL, 0, NULL, NULL);
    	char *pcBuf = new char[iReq];
    	WideCharToMultiByte(CP_ACP, 0, sIn.c_str(), -1, pcBuf, iReq, NULL, NULL);
    	string sTemp = pcBuf;
    	delete[] pcBuf;
    	return sTemp;
    }
    
    wstring ANSIToWide(string sIn) {
    	int iReq = MultiByteToWideChar(CP_ACP, 0, sIn.c_str(), -1, NULL, 0);
    	wchar_t *pcBuf = new wchar_t[iReq];
    	MultiByteToWideChar(CP_ACP, 0, sIn.c_str(), -1, pcBuf, iReq);
    	wstring sTemp = pcBuf;
    	delete[] pcBuf;
    	return sTemp;
    }
    
    tstring EnsureTStr(string sIn) {
    #ifndef UNICODE
    	return sIn;
    #else
    	return ANSIToWide(sIn);
    #endif
    }
    
    tstring EnsureTStr(wstring sIn) {
    #ifdef UNICODE
    	return sIn;
    #else
    	return WideToANSI(sIn);
    #endif
    }
    ...or something like that
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Hi parksie,
    Is that same as mbstowcs and wcstombs?

    regards,
    Chris.C

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Same effect, but different parameters (you still need a char* and wchar_t* buffer).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Thx, I got it.
    By the way, do you have any idea how to read tha data from a serial port? For instance, I successful open the COM1 and send a stream of string thtough the COM1, but I can read the data from the serial port.

    I've copy the sample from MSDN, and there have a WaitCommEvent, but when it read tha data from serial port, it store the data into a variable as BYTE. Then how can I display this into a proper text in my EDIT control?

    Also, do you have any idea of my previous post abt the CALLBACk function in DLL? CALLBACK

    regards,
    Chris.C

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you've compiled for ANSI () then use SetWindowText It'll look well weird and will most likely be truncated at the first zero byte. You could convert each byte to its hex representation using itoa and display it.

    As for callbacks, you need function pointers - can't remember the syntax now. I think I did an example once...will dig it out.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width