How to make my Hexadecimal spit out 8 digits(including leading zeros)

0

So, I wrote a function converting a decimal number into a hexadecimal number by using recursion, but I can't seem to figure out how to add the prefix "0x" and leading zeros to my converted hexadecimal number. Let's say I pass the number 18 into the parameters of my function. The equivalent hexadecimal number should be 0x00000012. However, I only end up getting 12 as my hexidecimal number. The same applies when I pass in a hexidecimal number 0xFEEDDAD. I end up getting only FEEDDAD without the prefix as my answer. Can someone please help me figure this out? I've listed my code below. Also, I'm only allowed to use fputc to display my output.

const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

void hexout (unsigned long number, FILE * stream) 
{
     long quotient;
     long remainder;

     quotient = number / 16;
     remainder = number % 16;


     if(quotient != 0)
       hexout(quotient,stream);


     fputc(digits[remainder],stream);
}
c
recursion
decimal
hex
asked on Stack Overflow Oct 8, 2013 by user2857532

3 Answers

1
void hexout (unsigned long number, FILE * stream) 
{
    fprintf(stream, "0x%08lX", number);
}

If you cannot use fprintf (neither sprintf), you can use this kind of code (no recursion, but a 8-chars array on the stack):

const char digits[] = "0123456789ABCDEF";

void hexout(unsigned long number, FILE * stream)
{
  unsigned long int input = number;
  unsigned long int quotient;
  unsigned long int remainder;
  unsigned short ndigit = 0;
  char result[8] = {0};

  // Compute digits
  do
  {
     quotient  = input / 16;
     remainder = input % 16;
     result[7-ndigit] = digits[remainder];
     input = quotient;
     ndigit++;
  }
  while (ndigit < 8);

  // Display result

  fputc('0', stream);
  fputc('x', stream);
  for (ndigit = 0; ndigit < 8; ndigit++)
  {
    fputc(result[ndigit], stream);
  }
}

Of course, this can be improved a lot...

answered on Stack Overflow Oct 8, 2013 by maqui • edited Oct 8, 2013 by maqui
0

Add digits to a string, and print out string with zero-padding using fprintf. Or just use fprintf to begin with.

0

Your own hexout fails for obvious reasons. You cannot 'continue' to output a number of zeroes when the value reaches 0, because you don't know how much numbers you already emitted. Also, you don't know when to prepend "0x" -- it should be before you start to emit hex digits, but how can you know you are at the start?

The logical way¹ to do this is to not use recursion, but a simple loop instead. Then again -- unsaid, but a fair bet this is a homework assignment, and in that case any number of silly constraints are possible ("write a C program without using the character '{'" comes to mind). In your case it's "you must use recursion".

You must add a counter to your recursive function; when it reaches 0, you know you have output 0x, and if it's not 0 you need to output a hex digit, irrespective if your value is 0 or not. There are a couple of ways of adding a counter to a recursive function: a global variable (which would be the easiest and utterly ugliest way, so please don't stop reading here), a static variable -- only semantically better than a global --, or a pass-by-reference argument (of which some say is a myth, but then again the end result is the same).

Which method is best for you depends on how well you can defend why you used that method.

¹ So is printf("0x%08X") an "illogical" solution? Yes. It solves the problem but without any further insights. The purpose of this assignment is not to find out the existence of printf and its parameters, it's to learn how (and why) to use recursion.

answered on Stack Overflow Oct 8, 2013 by Jongware • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0