|
-
Jul 16th, 2001, 04:50 AM
#1
Thread Starter
PowerPoster
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
-
Jul 16th, 2001, 04:55 AM
#2
Monday Morning Lunatic
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
-
Jul 16th, 2001, 05:02 AM
#3
Thread Starter
PowerPoster
Hi parksie,
Is that same as mbstowcs and wcstombs?
regards,
Chris.C
-
Jul 16th, 2001, 05:04 AM
#4
Monday Morning Lunatic
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
-
Jul 16th, 2001, 05:09 AM
#5
Thread Starter
PowerPoster
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
-
Jul 16th, 2001, 05:16 AM
#6
Monday Morning Lunatic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|