PI314159
PI314159

Reputation: 39

sprintf leading zero padding in C

The code below prints ffffffffff.

I need to the output to be 16 digits long 000000ffffffffff with leading zeros. The var1 and var2 variables can be different so it is not necessary that I want to pad 6 zeros only. I just need to output to be 16 digits with leading zeros. How should I proceed?

#include <stdio.h>

int main() {
    int var1 = 1048575;
    int var2 = 1048575;

    char buffer[100];

    sprintf(buffer, "%x%x", var1, var2);
    printf("%s\n", buffer);

    return 0;
}

Upvotes: 3

Views: 5365

Answers (6)

Gerard Rozsavolgyi
Gerard Rozsavolgyi

Reputation: 5064

You can also do some small calculus to compute the number of significant digits and then print the desired number of leading zeros as log_base_ 16(var) is the size of var in hexadecimal:

#include <stdio.h>
#include <math.h>

int main ()
{
    int var1 = 1048575;
    int var2 = 1048575;
    int padd = 16 - ceil(log(var1)/log(16))-ceil(log(var2)/log(16));
    char buffer [100];
    sprintf (buffer, "%0*x%x%x", padd, 0, var1, var2);
    printf("%s\n", buffer);

    return 0;
}

Upvotes: 0

chqrlie
chqrlie

Reputation: 144750

Here is a simple solution without an intermediary buffer:

#include <stdio.h>

int main(void) {
    int var1 = 1048575;
    int var2 = 1048575;

    printf("%0*x%x\n", 16 - snprintf(NULL, 0, "%x", var2), var1, var2);

    return 0;
}

There is a special case for which you might want to define the precise behavior: what if var2 == 0? The above code will print a trailing 0. If you want only significant digits printed, and therefore no digits for a zero value, you can modify the statement this way:

    printf("%0*x%x\n", 16 - snprintf(NULL, 0, "%.x", var2), var1, var2);

A small difference: adding a . between % and x to specify a precision field of 0 specifying the minimum number of digits as none.

Upvotes: 1

chux
chux

Reputation: 153488

Print var2 and then print the concatenation

#include <stdio.h>

int main(void) {
  int var1 = 1048575;
  int var2 = 1048575;

  char buffer[100];
  char bufvar2[sizeof var2 * CHAR_BIT + 1];
  int len = sprintf(bufvar2, "%x", var2);

  #define TOTAL_MIN_WIDTH 16
  sprintf(buffer, "%0*x%s", TOTAL_MIN_WIDTH - len, var1, bufvar2);
  printf("%s\n", buffer);

  return 0;
}

Upvotes: 0

4386427
4386427

Reputation: 44274

#include <stdio.h>

int main ()
{
    int var1=1048575;
    int var2=1048575;

    char buffer [17];
    char zeros [] = "0000000000000000";    
    sprintf (buffer, "%x%x", var1, var2);

    // Put the zeros in front
    int len = strlen(buffer);
    if (len < 16)
    {
        zeros[16-len] = 0x0; // Terminate the string at 16-len
    }
    else
    {
        zeros[0] = 0x0;  // Terminate the string at 0 as no extra zero is needed
    }

    printf("%s%s\n", zeros, buffer);

    return 0;
}

Upvotes: 1

David C. Rankin
David C. Rankin

Reputation: 84561

Another slight twist using a tmp buffer and a string (char array) of '0's:

#include <stdio.h>
#include <string.h>

enum { PADLN = 16, MAXC = 17 };

int main (void)
{
    int var1=1048575;
    int var2=1048575;

    char tmp [MAXC];
    char buffer [MAXC];
    char zeros[] = "0000000000000000";
    size_t len = 0;

    sprintf (tmp, "%x%x", var1, var2);
    len = strlen(tmp);
    zeros[PADLN - (len < PADLN ? len : PADLN)] = 0;
    sprintf (buffer, "%s%s", zeros, tmp);

    printf ("\n buffer : '%s'\n\n", buffer);

    return 0;
}

Output

$ ./bin/pad16

 buffer : '000000ffffffffff'

Upvotes: 0

molbdnilo
molbdnilo

Reputation: 66371

You could do it in two steps:

#include <stdio.h>

int main(void) {
    int var1 = 1048575;
    int var2 = 1048575;
    int length = 0;
    char buffer[100];
    char paddedbuffer[100] = "000000000000000";
    length = sprintf (buffer, "%x%x", var1, var2);
    char* here = length < 16 ? paddedbuffer + 16 - length : paddedbuffer;
    sprintf(here, "%s", buffer);
    printf("%s\n", paddedbuffer);
    return 0;
}

There's probably a more elegant solution somewhere.

Another possibility is converting your result into an integer and sprintfing that with a field width, or calculating the actual digits of the rightmost number first.

Upvotes: 1

Related Questions