Making C read blocks of arrays as little endian

Programming, for all ages and all languages.
Post Reply
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Making C read blocks of arrays as little endian

Post by Troy Martin »

Is there any way to have C read 16-bit words in an array of chars as little endian format on any architecture? I'm writing an emulator for a little-endian architecture I designed and I want it to act little-endian on any architecture it's compiled on.

example char array loaded from a file:

Code: Select all

char memory[1024] = {
0x10,0x28,0xF0,0xF0 };
The two bytes in the middle is a little-endian word. Therefore, when loaded out of memory and into a register (or the PC), it's the word 0xF028.

Thanks,
Troy
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Making C read blocks of arrays as little endian

Post by JamesM »

Cast the relevant area to a 16-bit integer and dereference. Thus:

Code: Select all

uint16_t intvalue = * (uint16_t)&memory[1];
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: Making C read blocks of arrays as little endian

Post by bewing »

JamesM wrote:Cast the relevant area to a 16-bit integer and dereference.
I'm afraid that won't work on quite a few occasions. First, that memory fetch would be misaligned in the example given, which may cause an alignment fault on x86 machines -- and may crash bigendian machines. Second, it won't give you the proper byte order on bigendian machines.

@Troy: since your example is misaligned, there is only one easy way to solve the problem. You must FORCE the byteorder of the result.
It is fortunate that your example is already in the form of a char array.

Code: Select all

	p = ptr to 1st byte of "16 bit word"
	little_endian_word = *p | (p[1] << 8);
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Making C read blocks of arrays as little endian

Post by ru2aqare »

There are lots of macros that do exactly this. For example, htons and htonl come to mind, but I'm sure there are others.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Making C read blocks of arrays as little endian

Post by Troy Martin »

@bewing: Thanks a ton, works great!
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Making C read blocks of arrays as little endian

Post by JamesM »

I crafted a reply to bewing, then looked over the problem description again and did notice that it explicitly mentioned endianness. So my reply is indeed unsafe - apologies.
Post Reply