| re: Working with Endianness
Tomás wrote:
[color=blue]
>
> Let's say you want to write fully portable code that will be writing files
> or sending data, and the data is text encoded using Unicode 16-Bit.[/color]
Do you mean utf-16 or ucs-2 or something else?
[color=blue]
> Endianness comes into play. I'm writing code at the moment which
> determines the Endianness of the architecture, and then converts values to
> Bigendian if they need to be converted.[/color]
Text that uses encodings with two or more bytes should start with a byte
order mark.
[color=blue]
> At the moment my code is based around:
>
> typedef unsigned short uint16;
>
> union {
> uint16 entire_integral_value;
> unsigned char bytes[2];
> };
>
>
> I set the value of each individual byte, and then test the value of
> "entire_integral_value" to see what way it's arranged in memory.
>
> I realise that it's Undefined Behaviour to store a value in a union via
> one type, and the access it via another type, but it's the only idea I
> have at the moment.
>
> And other ways of determining Endianess?[/color]
I think there is not much you can do other than rely on the byte order mark
or let the user be able to specify the endianness somehow.
[color=blue]
> (By the way, I think it's bullshit to note be able to access an unsigned
> integral type's bytes via a union...)[/color]
Well, a union was never meant for something like this. And there is always
reinterpret_cast. |