Re: BYTE to LPCTSTR ? by Robby
Robby
Sat Jun 24 12:43:02 CDT 2006
Hello Heinz and John!
and its a success!
Bytes can be read and printed on the screen or stored in an array!
I did try TextOutA, however when I typed the "(", intellisense didn't come
on! So I tried to look up the parameters in help, but I was unsuccessfull.
Probably, the parameters are the same as TextOut function but I just didn't
want to continue on a function I wasn't too sure about.... maybe later I will
look into it!
So I adopted the MultiByteToWideChar function, and it worked! At first there
was a glitch with the 3rd parameter, however, I type casted it to reflect a
LPCSTR as so:
MultiByteToWideChar(CP_ACP,0, (LPCSTR) abBuffer,30,szBuffer,30);
Although, as I read the description of the parameters, I didn't really
understand what they mean by the following explaination for the dwFlags
parameter though:
"[in] Indicates whether to translate to precomposed or composite-wide
characters (if a composite form exists), whether to use glyph characters in
place of control characters, and how to deal with invalid characters. You can
specify a combination of the following flag constants. "
[and]
"A composite character consists of a base character and a nonspacing
character, each having different character values. A precomposed character
has a single character value for a base/nonspacing character combination. In
the character è, the e is the base character and the accent grave mark is the
nonspacing character.
The function's default behavior is to translate to the precomposed form. If
a precomposed form does not exist, the function attempts to translate to a
composite form.
The flags MB_PRECOMPOSED and MB_COMPOSITE are mutually exclusive. The
MB_USEGLYPHCHARS flag and the MB_ERR_INVALID_CHARS can be set regardless of
the state of the other flags. "
I read it three (3) times... I guess I would need some definitions
dictionary concerning terms like, Precomposite, glyph and non-spacing
character!
I never ran into this before.... Please do not feel obligated to explain
this, I think you have done enough good for me.... Thanks for your help guys!
--
Appreciated regards
Roberto
"John Carson" wrote:
> "Robby" <Robby@discussions.microsoft.com> wrote in message
> news:F9DEAC9E-E541-4696-828E-00639B79BCE0@microsoft.com
> > Hi,
> >
> > I have the following line of code which reads bytes from an rs232
> > port: Please consider the following code:
> >
> > ============================================
> > DWORD dwBytesRead = 0;
> > BYTE abBuffer[100];
> > TCHAR szBuffer[100];
> >
> > MySerial.Read(abBuffer,sizeof(abBuffer),&dwBytesRead);
> > ============================================
> >
> > Now, I need to print abBuffer to the screen, god knows I tried !
> >
> > TextOut(hdc,100,100,abBuffer,100); //no good!
> > TextOut(hdc,100,100,TEXT(abBuffer),100); // no good!
> > TextOut(hdc,100,100,(char)abBuffer,100); //I didn't expect
> > this one to work!
> > TextOut(hdc,100,100,wsprintf(szBuffer,TEXT("%s")),abBuffer);
> > //Forget it! :(
>
>
> "no good" is not useful information. Telling us the actual error would be
> useful information. Does it not compile (if not, what error message do you
> get?), or output junk or..? I will assume that it doesn't compile.
>
> > By now I have looked for answers in Petzold, Osborn and SAM's books
> > and I now need help! >>>> a g a i n!
> >
> > Anyone!
> >
> > BYTE is typedefed in Windows as so,
> >
> > typedef unsigned char BYTE;
> >
> > -so this tells me that a byte is really defined as a char... right ?
> > -A char is 8 bits ... right ?
> >
> > So if the 4th parameter of TextOut takes a LPCTSTR, then how would I
> > convert this so that TextOut would accept it ?
>
> Text processing functions in Windows come in two versions: an "A" for "Ansi"
> version and a "W" for "wide character" version. Ignoring some complications,
> the A version is for 8 bit chars and the W version is for 16 bit Unicode
> wchar_t s. Thus there are both TextOutA and TextOutW.
>
> TextOut is a macro, defined as
>
> #ifdef UNICODE
> #define TextOut TextOutW
> #else
> #define TextOut TextOutA
> #endif
>
> Thus, depending on whether or not UNICODE is #defined, TextOut is either
> TextOutW or TextOutA.
>
> Similarly, depending on whether or not UNICODE is #defined, LPCTSTR is a
> typedef that is either LPCWSTR (for 16 bit wchar_t s) or LPCSTR (for 8 bit
> chars).
>
> I am guessing that your problem is that UNICODE is #defined, so you are
> getting TextOutW which takes a string of wchar_t s, whereas you are feeding
> it a string of chars.
>
> If so, you have two possibilities:
>
> (i) don't define UNICODE, or
> (ii) call TextOutA rather than TextOut, i.e., manually select the version of
> TextOut for 8 bit chars.
>
> There is a third possibility --- call MultiByteToWideChar to convert your 8
> bit chars to 16 bit wchar_t s --- but that seems like overkill unless you
> are going to be processing the string in more ways than just outputting it.
>
> --
> John Carson
>
>
>
>