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

x
?
Solved

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

Posted on 2004-10-27
5
Medium Priority
?
1,821 Views
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.
0
Comment
Question by:drumrboy44
[X]
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
5 Comments
 
LVL 8

Expert Comment

by:RLGSC
ID: 12428159
drumrboy44,

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)
0
 

Author Comment

by:drumrboy44
ID: 12428567
"Bob"

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
      {
      formatted.insert(0,'0');
      }
      else if(integer < 256 && integer > 15)  // if 2, etc.
      {
      formatted.insert(0,"00");
      }
      else if(integer < 16)
      {
      formatted.insert(0,"000");      
      }
      return formatted;  //return formatted hex string
}

Thanks daddy.
0
 
LVL 23

Expert Comment

by:brettmjohnson
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


0
 
LVL 55

Accepted Solution

by:
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);
0
 

Author Comment

by:drumrboy44
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...
0

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