• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

DEC,HEX conversion

Hi folks,

I have a question:

I am reading a binary file. If I use printf with %d, I will see the output as -1 -1 -1 59; if with %x, then ff ff ff 3b. The problem is that, -1 -1 -1 59 actually represents a value of -197. How can I get -197 from -1 -1 -1 59? Thanks

Thanks

(I use fread to read bytes from the file)

Urgent :-)
0
tiger0516
Asked:
tiger0516
3 Solutions
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi tiger0516,

It appears that you've read the individual bytes and need to pack them into an integer word.  This goes a lot smoother if you'll read them into an unsigned char buffer or cast a pointer to the buffer as unsigned.  C wants to sign extend and that sometimes gets messy.  Since this example has all 1 bits in the upper three bytes it's not critical, but as soon as any of those bits becomes a 0 sign extension is a problem.

Here's a bit of code that should help.

  char Buffer[4];
  unsigned char *uBuffer;
  int    iValue;

  fread (Buffer, 1, 4, Stream);

  uBuffer = (unsigned char*) Buffer;

//  If the system is big endian
  iValue = (uBuffer[0] << 24) | (uBuffer[1] << 16) | (uBuffer[2] < 8) (uBuffer[3]);

//  I f the system is little endian
  iValue = (uBuffer[3] << 24) | (uBuffer[2] << 16) | (uBuffer[1] < 8) (uBuffer[0]);


Good Luck!
Kent
0
 
tiger0516Author Commented:
Kent,

Many thanks! I think it should work. But there is an error whem compiling:

(using gcc )

Error part2.c 52: Call of non-function in function main
(line 52) iValue = (uBuffer[0] << 24) | (uBuffer[1] << 16) | (uBuffer[2] < 8) (uBuffer[3]);

Thank you.

0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi tiger0516,

[redfaced]

whoops ....  I dropped a character.  


//  If the system is big endian
  iValue = (uBuffer[0] << 24) | (uBuffer[1] << 16) | (uBuffer[2] < 8) | (uBuffer[3]);

//  I f the system is little endian
  iValue = (uBuffer[3] << 24) | (uBuffer[2] << 16) | (uBuffer[1] < 8) | (uBuffer[0]);

[/redfaced]

Kent
0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
sunnycoderCommented:
Hi Kent,

You missed another character

For big endian
iValue = (uBuffer[0] << 24) | (uBuffer[1] << 16) | (uBuffer[2] << 8) | (uBuffer[3]);
                                                                                           ^^ Shift and not comparison

For little endian
iValue = (uBuffer[3] << 24) | (uBuffer[2] << 16) | (uBuffer[1] < 8) | (uBuffer[0]);
                                                                                          ^^Shift and not comparison

;-)
0
 
cryptosidCommented:
Hi,

After reading in 4 byes..with..

fread (Buffer, 1, 4, Stream);


you could also use..

sscanf(Buffer,"%d",&num);

num should contain the number you seek.

pardon me if i got the syntax for sscanf() wrong.. haven't been doing practical coding lately...

Regards,
Sid
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Sunny,

What can I say?  In this time zone we're all tired and ready for bed.   :~}


Kent
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now