Re: sprintf: the format problem

From: Robert Stankowic (pcdoktor_at_netway.at)
Date: 12/19/03


Date: Fri, 19 Dec 2003 06:45:07 +0100


"Huey" <huey_jiang@yahoo.com> schrieb im Newsbeitrag
news:ae92bb50.0312182112.37f533b6@posting.google.com...
> Hi All,
>
> I saved hashed values of hex into buf[32], then buf becomes unreadable
> by the means of calling puts(buf), printf("%s") and so on. Then, I
> need to convert the hex in buf[] into a char buffer cbuf[32], by
> calling sprintf(). I did:
>
> sprintf(cbuf, "%x", buf);

Try
(CHAR_BIT == 8 assumed)

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
   int i;
   unsigned char buf[32] =
      {0, 1, 2, 3, 4, 5, 55, 66, 77};
   char cbuf[97];

   for(i = 0; i < 32; i++)
   {
      sprintf(cbuf + 3 * i, "%02x ", buf[i]);
   }
   puts(cbuf);
   return EXIT_SUCCESS;
}

>
> No error! Just no working. Well, I used sprintf() a lot in format "%s"
> "%d", and liked sprintf. It is my first time of using it in format
> "%x". I wonder does format affect sprintf? Anybody can help me to get
> this trick? Thanks!

from N869:
o,u,x,X The unsigned int argument is converted to unsigned octal (o),
unsigned
decimal (u), or unsigned hexadecimal notation (x or X) in the style dddd;
the
letters abcdef are used for x conversion and the letters ABCDEF for X
conversion. The precision specifies the minimum number of digits to appear;
if the value being converted can be represented in fewer digits, it is
expanded
with leading zeros. The default precision is 1. The result of converting a
zero value with a precision of zero is no characters.



Relevant Pages

  • Re: double to int conversion yields strange results
    ... giving me two different values for the int. ... the question raised is not about the precision of the ... is done in extended precision. ... It is the conversion to 'double' before the conversion to 'int' that ...
    (comp.lang.c)
  • Re: %a in printf
    ... where there is one hexadecimal digit (which is ... precision; if the precision is missing and FLT_RADIX ... used for a conversion and the letters ABCDEF for A ... If the value is zero, ...
    (comp.lang.c)
  • Re: Numerical accuracy of C++ and Fortran programs on 32 bit machines
    ... low-order binary digits are set to zero which results in the case as ... is "more" correct than the zero lower significant binary digits. ... The lesson is, if in Fortran one wants full double precision, specify it ... conversion of a decimal number does not ...
    (comp.programming)
  • Re: increasing width
    ... the compiler will do an automatic conversion of either ... The problem is that zero is correctly ... represented in any precision on any existing machine. ... tell you that the standard allows a compiler ...
    (comp.lang.fortran)
  • Re: My scripting language - any suggestions?
    ... there will be a problem with precision. ... anything from Float it also does the property of Float being an ... Now what will happen if one argument is int and another ... conversion to int will not work ...
    (comp.compilers)