Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading little-endian format numbers 1

Status
Not open for further replies.

AtomicChip

Programmer
May 15, 2001
622
CA
Alright... I'm a little confused (..not to mention tired, which may be a driving factor behind my not being able to easily figure this out right now..), but how would you go about doing this (using only c-runtime stuff... no stl or anything please!):

I'm reading the following from a file:
Code:
FA A3 14 01

This is stored in little endian format. How would I go about 1) Reading the data in (I'm storing it in a char* right now), and 2) Convert it to an unsigned long in big endian format?

Thanks in advance,
AtomicChip

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Off the top of my head, use a union of char[ 4 ] and a long.

Assuming that this is on a pc, then that should do the trick.

I think that it goes something like:

char[ 0 ] //ls byte of long, takes the 1st byte of your number
char[ 1 ] // takes 2nd byte of your number
char[ 2 ] // takes 3rd byte
char[ 3 ] //ms byte of long: takes 4th byte.

It's been a while since I've needed to do this...

rgds
Zeit.
 
Code:
#include<stdio.h>

int main()
{
union  {
char i[4];
long l;
}fred ;

int i;

fred.l = 0x12345678;
for ( i = 0; i<4; i++ )
{
printf( "%3x\n", fred.i[ i ]);

}
return 0;
}

shows you what I mean...
 
> 1) Reading the data in
Use strtoul() to convert your character pairs into a numeric value.

> 2) Convert it to an unsigned long in big endian format?
Well little endian would be
Code:
unsigned long result = 0;
for ( i = 0 ; i < 3 ; i++ ) {
  // convert the i'th value, store in c
  c = c << (8*i);
  result = result | c;
}

Big endian
Code:
unsigned long result = 0;
for ( i = 0 ; i < 3 ; i++ ) {
  // convert the i'th value, store in c
  result = result << 8;
  result = result | c;
}

--
 
Thanks all for your posts...

Upon further investigation, I've found that my problem wasn't so much the conversion (I ended up going with this instead):

Code:
int x = ((i&0xFF000000)>>24) + ((i&0x00FF0000)>>8) + ((i&0x0000FF00)<<8) + ((i&0x000000FF)<<24);

My problem's more about reading the file value. Let's say I'm reading the first four bytes of a file which happen to be
Code:
00 00 00 FF

...And I want to turn this into an int (the value, of which, should be 255, right?). Well, I'm getting a bunch of numbers, but none of them are the right one! :p

What the hell am I doing wrong??!!

Thanks again,
- AtomicChip

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Show the code to read the values from the file, and to assemble an int.

Curious that you should post a byte swapper, which may have a hidden bug if 'i' is a signed (rather than unsigned) quantity.




--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top