Decimal to hex.. with leading zeros/4 digit format

Posted on 2004-10-27
Last Modified: 2008-03-04
I want to take an integer, convert it to hex, but retain the correct number of leading zeros, for a final hex number that is 4 digits long, so if the number is 6FF, I want it to display 06FF, and if it is F, 000F. Basically Just format the number to 4 digits. Thanks in advance.
Question by:drumrboy44
    LVL 8

    Expert Comment


    I must admit that this sounds suspiciously like a homework problem in a basic programming course, which is, of course, prohibited on EE.

    You convert the number by successively using the mod and integer division operators.  The length computation is similarly straightforward.

    If you are just writing a program in "C", take a look at printf and the operator for hex conversion, with zero fill, you would need to specify the field width of the output.

    I admit the above is cryptic, but in the event that this is homework, I have still left you with some work to do.

    - Bob (aka RLGSC)

    Author Comment


    While I dont understand your motivations for unprovoked suspicions, I will go as far as to prove you wrong.  I had to write several bit modification functions, and I was hoping someone would clue me into a "format" function ala Visual Basic "Format" function that allows for specification of digits.  I went ahead and wrote the function the only way I knew how, but still... thanks for nothing.  

    string converter(int integer)
                    string formatted;    
          char buffer[16];   //creates char array where we will copy hex number to

          _itoa(integer, buffer, 16);  //converts to hex, (integer to convert, array, radix)
                    formatted = buffer;    //sets string equal to hex number, and allows for .insert

          if(integer < 4096 && integer > 255)  //if 3 digit hex number add 1 0
          else if(integer < 256 && integer > 15)  // if 2, etc.
          else if(integer < 16)
          return formatted;  //return formatted hex string

    Thanks daddy.
    LVL 23

    Expert Comment

    That seems to be a whole lot of work.  Why not use printf()?

    int num = 0xF;
    printf("%04x\n", num);

    The format string means:
    x - hex output
    4 - 4 character wide field
    0 - pad with leading zeros

    LVL 55

    Accepted Solution

    expanding brett's answer, just have to write to a string with sprintf() rather than printf():

    char buffer[5];  // buffer to receive hex number, set to proper maximum size including trailing null character

    int num = 0xF;
    sprintf(buffer, "%04x", num);

    if you want to have uppercase hex digits, use:

    sprintf(buffer, "%04X", num);

    Author Comment

    i couldnt use printf b/c i needed to store the string, not just output, but I actually did mean to split points between you two... sorry...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    Title # Comments Views Activity
    Help with my python script 6 114
    printing double sided for a pdf document 19 74
    post4 challenge 28 57
    seriesUp challenge 7 53
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    856 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