###Use variable width format specifier###
Use variable width format specifier
When using sprintf
, you can use a variable width specifier, and also a "pad with zero" specifier, so that you don't need to do these things manually:
void make_hex_string_easy(unsigned int invokeid, char** xref)
{
int len = num_hex_digits(invokeid);
/* if odd number, add 1 - zero pad number */
if(len % 2 != 0)
len++;
sprintf(*xref, "%0*X", len, invokeid);
}
Here, after the %
character, the 0
tells sprintf
to pad the hex value with zeros. For example:
printf("%08x", 0x1234); // Will print "00001234" due to the %08x specifier
After the 0
there is a *
character. This tells sprintf
that the width will come from a variable instead of being hardcoded. For example:
printf("%0*x", 6, 0x1234); // Will print "001234" with a width of 6
So putting it all together, "%0*X"
prints a zero padded hex string whose width comes from the next argument. In the modified code above, that argument is len
.
###Use variable width format specifier###
When using sprintf
, you can use a variable width specifier, and also a "pad with zero" specifier, so that you don't need to do these things manually:
void make_hex_string_easy(unsigned int invokeid, char** xref)
{
int len = num_hex_digits(invokeid);
/* if odd number, add 1 - zero pad number */
if(len % 2 != 0)
len++;
sprintf(*xref, "%0*X", len, invokeid);
}
Here, after the %
character, the 0
tells sprintf
to pad the hex value with zeros. For example:
printf("%08x", 0x1234); // Will print "00001234" due to the %08x specifier
After the 0
there is a *
character. This tells sprintf
that the width will come from a variable instead of being hardcoded. For example:
printf("%0*x", 6, 0x1234); // Will print "001234" with a width of 6
So putting it all together, "%0*X"
prints a zero padded hex string whose width comes from the next argument. In the modified code above, that argument is len
.
Use variable width format specifier
When using sprintf
, you can use a variable width specifier, and also a "pad with zero" specifier, so that you don't need to do these things manually:
void make_hex_string_easy(unsigned int invokeid, char** xref)
{
int len = num_hex_digits(invokeid);
/* if odd number, add 1 - zero pad number */
if(len % 2 != 0)
len++;
sprintf(*xref, "%0*X", len, invokeid);
}
Here, after the %
character, the 0
tells sprintf
to pad the hex value with zeros. For example:
printf("%08x", 0x1234); // Will print "00001234" due to the %08x specifier
After the 0
there is a *
character. This tells sprintf
that the width will come from a variable instead of being hardcoded. For example:
printf("%0*x", 6, 0x1234); // Will print "001234" with a width of 6
So putting it all together, "%0*X"
prints a zero padded hex string whose width comes from the next argument. In the modified code above, that argument is len
.
###Use variable width format specifier###
When using sprintf
, you can use a variable width specifier, and also a "pad with zero" specifier, so that you don't need to do these things manually:
void make_hex_string_easy(unsigned int invokeid, char** xref)
{
int len = num_hex_digits(invokeid);
/* if odd number, add 1 - zero pad number */
if(len % 2 != 0)
len++;
sprintf(*xref, "%0*X", len, invokeid);
}
Here, after the %
character, the 0
tells sprintf
to pad the hex value with zeros. For example:
printf("%08x", 0x1234); // Will print "00001234" due to the %08x specifier
After the 0
there is a *
character. This tells sprintf
that the width will come from a variable instead of being hardcoded. For example:
printf("%0*x", 6, 0x1234); // Will print "001234" with a width of 6
So putting it all together, "%0*X"
prints a zero padded hex string whose width comes from the next argument. In the modified code above, that argument is len
.