Other HI-TECH Compilers & Products >> Other HI-TECH Compilers & Products

Pages: 1
Tim Noyce



Reged: Sep 02 2005
Posts: 1
Byte order of unsigned longs
      #18720 - Fri Sep 02 2005 11:02 AM

I am using the HI-Tech C for the 68HC11E9, v 7.10b

I think this is a pretty basic question, but I cannot find
any information that seems to detail this.

I am curious as to what order the bytes are stored in, when using unsigned longs.

The reason this is important, is that I have to take data
that is presented in 4 bytes (each an unsigned char), and
convert it into the correct internal binary representation.

This is because the data has to be used to find the location of the information in a hash table, so I need to be certain that the index which is generated when the data is inserted, is the same as the index which will be generated, when the lookup is requested, using an unsigned long which is in the correct order.

For example:

Number is 558166918.

I receive the following hex bytes, in this order:

21 44 F3 86

If these bytes are put into an unsigned long, does this equate to 558166918?

If not, if anyone would be able to tell me how best to
get this information into the correct order, I'd appreciate
it.

Thanks in advance
-Tim


Post Extras: Print Post   Remind Me!   Notify Moderator  
Mark Pappin

***

Reged: Nov 01 2004
Posts: 599
Loc: Brisbane, Australia
Re: Byte order of unsigned longs [Re: Tim Noyce]
      #18723 - Fri Sep 02 2005 05:23 PM

Quote:

I am curious as to what order the bytes are stored in, when using unsigned longs.




The C standard doesn't mandate an order, and well-written code shouldn't depend on any particular order.

Quote:

I receive the following hex bytes, in this order:

21 44 F3 86

If these bytes are put into an unsigned long, does this equate to 558166918?




Depends on the way they are 'put into an unsigned long'.

Quote:

If not, if anyone would be able to tell me how best to get this information into the correct order, I'd appreciate it.




Code:

unsigned long l;
unsigned char buf[4] = {0x21, 0x44, 0xf3, 0x86};
l = (unsigned long)buf[0] << 24 |
    (unsigned long)buf[1] << 16 |
    (unsigned long)buf[2] << 8 |
    buf[3];


This code will work with any C compiler, from any vendor, targetting any chip.

--------------------
Mark Pappin - HI-TECH Software


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 2 anonymous users are browsing this forum.

Moderator:  ndouglas, jtemples, Dan Henry, Andrew L, mikerj, dave g, meisty, josh stevo 

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is enabled
      UBBCode is enabled

Rating:
Topic views: 2289

Rate this topic

Jump to

Contact Us | Privacy statement HI-TECH Software

Powered by UBB.threads™ 6.5.5