[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • 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


(I use fread to read bytes from the file)

Urgent :-)
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!
tiger0516Author Commented:

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.

Kent OlsenData Warehouse Architect / DBACommented:
Hi tiger0516,


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]);


Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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


After reading in 4 byes..with..

fread (Buffer, 1, 4, Stream);

you could also use..


num should contain the number you seek.

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

Kent OlsenData Warehouse Architect / DBACommented:
Hi Sunny,

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


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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