Discussion:
Accessing "Hi" and "Lo" Byte of wchar_t
(too old to reply)
Jack
2009-08-05 08:07:08 UTC
Permalink
Thanks for Ulrich bro for his advices. Now I know,
I have to swap the buffer to get the proper values
I wonder too, how do I access the "Hi" and "Lo" Byte of
wchar_t

wchar_t tem;
for (int i = 0; i < MAX_CHAR; i+=2)
{
HIBYTE(tem) = m_pBuffer[i];
LOBYTE(tem) = m_pBuffer[i+1];
}

This is simply dumb, because of the l-value stuff
How do I access the lo-byte and hi-byte individually
Thanks
Jack
Jack
2009-08-05 08:16:03 UTC
Permalink
I am after marshalling of wchar_t's from char's
Thanks
Jack
David Webber
2009-08-05 08:43:14 UTC
Permalink
Post by Jack
Thanks for Ulrich bro for his advices. Now I know,
I have to swap the buffer to get the proper values
I wonder too, how do I access the "Hi" and "Lo" Byte of
wchar_t
wchar_t tem;
for (int i = 0; i < MAX_CHAR; i+=2)
{
HIBYTE(tem) = m_pBuffer[i];
LOBYTE(tem) = m_pBuffer[i+1];
}
This is simply dumb, because of the l-value stuff
How do I access the lo-byte and hi-byte individually
Windows supplies the MAKEWORD() macro for concatenating two BYTEs into a
WORD.

But putting two random chars into a wchar_t like this looks like madness to
me.

Ulrich's advice on byte swapping was about big-endian UTF16. Is that what
you have?

WORD w1 = ...;
WORD w2 = MAKEWORD( HIBYTE(w1), LOBYTE(w1) );

is one way of swapping the bytes of a word. But where on earth have you
found big-endian UTF-16?

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
Jack
2009-08-05 09:18:27 UTC
Permalink
Post by David Webber
is one way of swapping the bytes of a word. But where on earth have you
found big-endian UTF-16?
Thanks for the hints....
I am not sure... but I looked it up in the UNICODE map
and compare to my memory locations, and simply found them the other way
round.
Jack
Post by David Webber
Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
David Webber
2009-08-05 09:48:29 UTC
Permalink
Post by Jack
Post by David Webber
is one way of swapping the bytes of a word. But where on earth have
you found big-endian UTF-16?
Thanks for the hints....
I am not sure... but I looked it up in the UNICODE map
and compare to my memory locations, and simply found them the other way
round.
Take care! Windows running on Intel Chips (and similar) is
"little-endian". Which means the low byte will come first. Which means
the number 0x0102 will be stored with byte 0x02 before 0x01. You do not
have to worry about this except in very specific (and relatively unusual)
circumstances. It is all handled automatically way down at the chip
level.

[The exceptional circumstances arise (a) when you need to take data files
written by a Windows program on a PC, and interpret them on a big-endian
computer (ie not a PC), or vice versa, or (b) when a specific data format
specifies big-endian-ness so that it is machine independent - MIDI is an
example.]

So I think you're probably seeing little-endian data on your computer and
comparing with a big-endian table (as humans read numbers from sheets of
paper in big endian format). If so, you do not need to swap any bytes.

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
Tim Roberts
2009-08-06 04:12:01 UTC
Permalink
Post by David Webber
Take care! Windows running on Intel Chips (and similar) is
"little-endian".
Actually, Windows is little-endian on ALL chips, and that includes all
versions -- even CE.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
David Webber
2009-08-06 07:34:22 UTC
Permalink
Post by Tim Roberts
Post by David Webber
Take care! Windows running on Intel Chips (and similar) is
"little-endian".
Actually, Windows is little-endian on ALL chips, and that includes all
versions -- even CE.
Thanks for that. I didn't know. (I was confining my remarks to systems I
was fairly sure about rather than attempting to imply something else might
pertain on other hardware.)

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
Jack
2009-08-05 09:52:32 UTC
Permalink
I am getting there, I see the right text on the tooltip after swapping but
when I come to this final step, it is in the wrong order again... is it what
SetWindowTextW does?

GetDlgItem(IDC_RECEIVE)->SetWindowTextW((LPCWSTR) pDoc->m_buffer);

Thanks for any hints
Jack
David Webber
2009-08-05 19:14:13 UTC
Permalink
Post by Jack
I am getting there, I see the right text on the tooltip after swapping but
when I come to this final step, it is in the wrong order again... is it
what SetWindowTextW does?
GetDlgItem(IDC_RECEIVE)->SetWindowTextW((LPCWSTR) pDoc->m_buffer);
Thanks for any hints
Windows APIs which accept Unicode strings use wchar_t. The internal
representation is little-endian. You don't have to worry about it at all.
You don't have to swap bytes - unless you have got a big-endian UTF-16
string from somewhere very peculiar - a non-PC machine (which I'm assuming
you haven't as you haven't mentioned it).

If you want to create a wchar_t string from a char string, use the API
provided - MultiByteToWideChar( ). Do not mess about with individual
bytes.

SetWindowTextW() takes a wchar_t string. The mystery, if you've been
swapping bytes, is how you got the tooltip to look right: my guess is that
you didn't define it with the byte-swapped string.

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
Loading...