Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2004-10-27
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 12428159

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

ID: 12428567

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

ID: 12428700
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

Jaime Olivares earned 2000 total points
ID: 12429012
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

ID: 12446435
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...

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …
Introduction to Processes

609 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