DEC,HEX conversion

Posted on 2006-05-18
Last Modified: 2010-04-15
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 :-)
Question by:tiger0516
    LVL 45

    Expert Comment

    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!
    LVL 1

    Author Comment


    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.

    LVL 45

    Accepted Solution

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


    LVL 45

    Assisted Solution

    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

    LVL 5

    Assisted Solution


    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...

    LVL 45

    Expert Comment

    Hi Sunny,

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


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now