Hi i have a program that outputs a bunch of hexadecimal numbers. Here is a example:
18
3048
ffff8007

The thing is i want all the outputs to be 8 digits long, so i want the out put to be something like:

00000018
00003048
ffff8007

so yea basically i just need to add some 0s infront the outputs that are not 8 digits long. Any ideas?

Thanks

Well depending on how you're outputting those values, this can be done in a few different ways. But if the values are c-strings before you output them, passing them to a simple function like this should work:

#include <stdio.h>

void fixed_print(char *text, int minChars, char fill) {
  int i;
  for (i = minChars - strlen( text ); i > 0; --i)
    putchar( fill );
  printf( text );
}

int main() {
  fixed_print("18", 8, '0');
  putchar('\n');

  fixed_print("3048", 8, '0');
  putchar('\n');

  fixed_print("FFFF8007", 8, '0');
  putchar('\n');

  return 0;
}

00000018
00003048
FFFF8007

Hope this helps.

Comments
"%08x" would have been a lot simpler IMO

Salem also made a good point in my reputation, I miss him actually being able to post :icon_rolleyes:
I don't use C very often, and very rarely use printf, but as he pointed out, you can do something like this instead:

#include <stdio.h>

int main() {
  printf("%08s\n", "18");
  printf("%08s\n", "3048");
  printf("%08s\n", "ffff8007");

  return 0;
}

Salem also made a good point in my reputation, I miss him actually being able to post :icon_rolleyes:
I don't use C very often, and very rarely use printf, but as he pointed out, you can do something like this instead:

#include <stdio.h>

int main() {
  printf("%08s\n", "18");
  printf("%08s\n", "3048");
  printf("%08s\n", "ffff8007");

  return 0;
}

I don't understand why you are printing text strings instead of hexadecimal representations of int or unsigned int values (see OP: where are strings there? ).
So the correct answer is: reread printf specifications and do as follows:

unsigned value; /* or int value */ 
...
printf("... %08x ... ",..,value,...);

I don't understand why you are printing text strings instead of hexadecimal representations of int or unsigned int values (see OP: where are strings there? ).
So the correct answer is: reread printf specifications and do as follows:

I was only giving a solution to outputting strings with a fixed number of characters, I did say "if the values are c-strings before you output them" to be fair :icon_rolleyes: and gave a better alternative method to do so.

#include <stdio.h>

int main() {
  printf("%08s\n", "18");
  printf("%08s\n", "3048");
  printf("%08s\n", "ffff8007");

  return 0;
}

> Just a little remark on the int main() line of your code: If a function doesn't require any arguments in C you have to put the void keyword between the function's '(' and ')' brackets (if you really want to be correct :P), like this: int main(void) :)

> This is a difference between C and C++, as in C++ this means that the function doesn't require any arguments
(if you don't type something between the brackets), in C this only means that the function can have unlimited arguments of any type ...

If a function doesn't require any arguments in C you have to put the void keyword between the function's '(' and ')' brackets (if you really want to be correct :P), like this: int main(void)

You don't have to, but it's a good idea for the sake of consistency and to avoid subtle problems. If you don't have a prototype for the function, the definition will only act as a prototype if the parameter list is not empty:

void foo() {}

int main ( void )
{
    foo();
    foo ( 10, 2.5, "booga" ); /* Compiles! */
}
void foo ( void ) {}

int main ( void )
{
    foo();
    foo ( 10, 2.5, "booga" ); /* Doesn't compile */
}
This article has been dead for over six months. Start a new discussion instead.