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
Printable View
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
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:...or something like that :)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
}
Hi parksie,
Is that same as mbstowcs and wcstombs?
regards,
Chris.C
Same effect, but different parameters (you still need a char* and wchar_t* buffer).
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
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.