Connecting Tech Pros Worldwide Help | Site Map

Working with Endianness

  #1  
Old March 14th, 2006, 04:05 PM
Tomás
Guest
 
Posts: n/a

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.

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.

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?

(By the way, I think it's bullshit to note be able to access an unsigned
integral type's bytes via a union...)

-Tomás
  #2  
Old March 14th, 2006, 04:15 PM
Jakob Bieling
Guest
 
Posts: n/a

re: Working with Endianness


Tomás <NULL@NULL.NULL> wrote:
[color=blue]
> 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]

Copying the contents of an int into a large enough array of unsigned
char (or char?) using memcpy (and accessing the char array afterwards)
should be valid. Don't quote me on that tho .. it's just in my mind
having read about this before.

hth
--
jb

(reply address in rot13, unscramble first)


  #3  
Old March 14th, 2006, 04:25 PM
Rolf Magnus
Guest
 
Posts: n/a

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.

  #4  
Old March 14th, 2006, 04:35 PM
Victor Bazarov
Guest
 
Posts: n/a

re: Working with Endianness


Jakob Bieling wrote:[color=blue]
> Tomás <NULL@NULL.NULL> wrote:
>
>[color=green]
>>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]
>
>
> Copying the contents of an int into a large enough array of unsigned
> char (or char?) using memcpy (and accessing the char array afterwards)
> should be valid. Don't quote me on that tho .. it's just in my mind
> having read about this before.[/color]

Yep, I see no problem with that either. The union trick OTOH is illegal.

V
--
Please remove capital As from my address when replying by mail
  #5  
Old March 14th, 2006, 07:15 PM
Maxim Yegorushkin
Guest
 
Posts: n/a

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]

Why don't choose utf-8 then? This way you don't have to deal with
byte-sex here. BTW, utf-8 is a defacto standard for Linux and I guess
other unixes, so that your fully portable code is portable across nixes
from the start.

  #6  
Old March 16th, 2006, 12:15 PM
Ramki
Guest
 
Posts: n/a

re: Working with Endianness


The following code snippet may help to find Endianness :

int x = 1;
if(*(char *)&x == 1)
printf("little-endian\n");
else printf("big-endian\n");

Thanx,
Rama

  #7  
Old March 18th, 2006, 12:05 AM
ljk
Guest
 
Posts: n/a

re: Working with Endianness


#define LITTLE_ENDIAN 0
#define BIG_ENDIAN 1

int machineEndianness()
{
int i = 1;
char *p = (char *) &i;
if (p[0] == 1) // Lowest address contains the least significant byte
return LITTLE_ENDIAN;
else
return BIG_ENDIAN;
}

It's not very efficient, but still works fine.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with pyserial and sending binary data? Rich answers 1 June 27th, 2008 05:23 PM
Re: Help with pyserial and sending binary data? Gabriel Genellina answers 0 June 27th, 2008 05:23 PM
How does C handle issues arising out of Endianness? Indian.croesus@gmail.com answers 18 January 3rd, 2007 07:55 PM
Variadic functions calling variadic functions with the argument list, HLL bit shifts on LE processors Ross A. Finlayson answers 19 November 14th, 2005 07:59 PM