Convert Large number to hex in C

Posted on 2009-04-22
Last Modified: 2013-12-26
sprintf(myHex, "%x", myInt); works for small numbers but is there a way to convert large numbers (larger than 2^32) to hex string or number in C?

Thank you!
Question by:nscappdev
    LVL 39

    Expert Comment

    Try the code below...

    Please note this is Windows specific, the C standard only specifics %x and %X for formatting hex numbers.
    	unsigned long long ull = 0xFFFFFFFFFFFFFFFF;
    	printf("%I64x", ull);

    Open in new window

    LVL 39

    Expert Comment

    Strictly speaking, you might need to add ULL as a suffix to the number to tell the compiler it's a 64bit number and not an int type.

    unsigned long long ull = 0xFFFFFFFFFFFFFFFFULL;
    LVL 49

    Expert Comment

    It's also pretty easy to break a 64-bit value into two 32-bit values, and then output the combined series of  16 hex digits.  If you need to see code to do that, let me know.

    Author Comment

    Thanks for the answers. I wish it could be that straightforward.  I'm using Informix ESQL/C. So unsigned long long and format specifier %I64x doesn't work for me. I'm using double to hold the large number. I am looking for a solution that can covert that large double number to hex

    Thank you

    LVL 49

    Accepted Solution

    Attached code seems to work, at least for positive values.
    #include <stdio.h>
    #include <math.h>
    int main (int argc, char *argv[])
    	double d= 0x123456789abcde;
    	unsigned long ulLo= long( fmod(d,0x100000000) );
    	unsigned long ulHi= long(    d / 0x100000000  );
    	printf("%08x\r\n", ulLo);
    	printf("%08x\r\n", ulHi);
    	printf("%08x%08x\r\n", ulHi, ulLo);

    Open in new window


    Author Comment


    Your solution worked! Thanks!

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    746 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

    16 Experts available now in Live!

    Get 1:1 Help Now