"Jamie" <> wrote:
> And what is the best strategy for when wchar_t != 2? I'm running linux
> on x86 and ppc hardware and find wchar_t = 4. I am looking for a clever
> way of defining a two element char (i.e. Java unicode representation)
> that isn't too reliant on hardware
Well, the type 'unsigned short' is probably two bytes on your system.
However the best way to read the values, given that the endianness is
probably different between your x86 and ppc hardware, is probably to
read unsigned chars and load them into wchar_t by shifting the value
like this:
/* define type twobyte as an array of 2 unsigned char */
typedef unsigned char twobyte[2];
wchar_t wid[32];
twobyte tbid[32];
fread(tbid, sizeof(twobyte), 32, fp);
for(i = 0; i < 32; i++)
{
wid[i] = tbid[i][0] << 8 + tbid[i][1]; /* Or the other way around */
}
wcstombs(hd->id, wid, 32);
Or, if you know that there can't be any characters outside the first 256
code points, therefore the high byte of the unicode representation is
always zero, then you could forget about all the wcstombs crap, and just
copy the low byte, either tbid[i][0] or tbid[i][1], into an array of char.
--
Simon.