• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1895
  • Last Modified:

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

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
drumrboy44
Asked:
drumrboy44
1 Solution
 
RLGSCCommented:
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
 
drumrboy44Author Commented:
"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
 
brettmjohnsonCommented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
drumrboy44Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now