Re: %x question

From: Jonathan Burd (jonathan.burd_at_REMOVEMEgmail.com)
Date: 01/30/05


Date: Sun, 30 Jan 2005 23:09:27 +0530

Carramba wrote:
>
> hi!
>
> int digit = 12;
> printf("%x\n", digit);
> running this code I get 'c' as output? that is the problem?

%x is a conversion specifier that makes any of the printf
family of functions to print numbers in hexadecimal.

---
§7.19.6.1#8
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.
---
<ot>
Decimal   Hexadecimal    Binary
-------------------------------
       0             0      0000
       1             1      0001
       2             2      0010
       3             3      0011
       4             4      0100
       5             5      0101
       6             6      0110
       7             7      0111
       8             8      1000
       9             9      1001
      10             A      1010
      11             B      1011
      12             C      1100
      13             D      1101
      14             E      1110
      15             F      1111
Visualize an odometer when reading the above table.
(An odometer records the distance that a vehicle travels.)
Read up on hexadecimal and learn to count in
these number systems.
Decimal     -- base-10
Hexadecimal -- base-16
Binary      -- base-2
All this means is that decimal has 10 digits
that one can use to represent numbers. Binary uses 2 digits.
Hexadecimal uses 16 digits to represent numbers (0 through F).
Yes, A, B, C, D, E, and F are treated as digits in hex.
The last digit in decimal is 9, that in binary is 1, and
that in hexadecimal is F. "10" in any number system
always comes after the last digit. This means
decimal 10 is not the same as hex 10 or binary 10.
Remember never to mix number systems in operations.
Suggested reading:
http://mathworld.wolfram.com/Base.html
http://mathworld.wolfram.com/Hexadecimal.html
http://mathworld.wolfram.com/Binary.html
</ot>
Regards,
Jonathan.
-- 
"We must do something.  This is something.  Therefore, we must do this."
  - Keith Thompson


Relevant Pages

  • Re: How to get decimal form of largest known prime?
    ... but I learned from it much about Python syntax. ... here code of a bigDecReprOfInt class as a kind of module and ... i.e. 4096 and not as I would expect it 80 decimal digits long ... # and strconversion for very large integers i. ...
    (comp.lang.python)
  • Re: Any progress yet? (was Re: Fast pi program?)
    ... conversion to base 26, ... Base 2^32 would /reduce/ the number of digits by a factor of about 1.7 ... numbers approximated with floating point is even worse.), the "pyramid ... it does cancel the performance gain if there was any. ...
    (comp.programming)
  • Re: Any progress yet? (was Re: Fast pi program?)
    ... conversion to base 26, ... you need to compute 2x the digits then convert to 26 (since the ... If you pack 4 elements and you use 8 primes ... numbers approximated with floating point is even worse.), the "pyramid ...
    (comp.programming)
  • Re: Clean Decimal Strip and Re-insert
    ... So if the user had entered 2, all these conversion would need to take place: ... Which is 2 digits to the left. ... If you can be sure the number of digits to the left of the decimal point that the user specifies will be greater than or equal to zero, then this will solve your question... ... Function Reformat(Number As Variant, DigitsLeft As Long) ...
    (microsoft.public.vb.general.discussion)
  • Re: Singles to Doubles
    ... I'm guessing that VB calculates its Singles and Doubles using some kind of power series expansion... ... Now, to continue my guessing, I think 15 digits were calculated for the Single and stored; but, because VB knows it is a Single, it only reports 6 of 7 digits when asked. ... Now, for the CStr conversion, I think VB is grabbing only the 6 or 7 digits it would normally use for display. ...
    (microsoft.public.vb.general.discussion)

Loading