Non-decimal radices/Output

From Rosetta Code
Revision as of 05:14, 31 January 2009 by rosettacode>Paddy3118 (New page: Programming languages often have built-in routines to convert a non-negative integer for printing in different number bases. Such common number bases might include binary, octal and [[Hexa...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Programming languages often have built-in routines to convert a non-negative integer for printing in different number bases. Such common number bases might include binary, octal and Hexadecimal.

Show how to print a small range of integers in some different bases, as supported by standard routines of your programming language. (Note: this is distinct from Number base conversion as a user-defined conversion function is not asked for)

Python

Binary (b), Octal (o), Decimal (d), and Hexadecimal (X and x) are supported by the formatmethod of a string <python>>>> for n in range(34): print " {0:6b} {1:3o} {2:2d} {3:2X}".format(n, n, n, n)


     0   0  0  0
     1   1  1  1
    10   2  2  2
    11   3  3  3
   100   4  4  4
   101   5  5  5
   110   6  6  6
   111   7  7  7
  1000  10  8  8
  1001  11  9  9
  1010  12 10  A
  1011  13 11  B
  1100  14 12  C
  1101  15 13  D
  1110  16 14  E
  1111  17 15  F
 10000  20 16 10
 10001  21 17 11
 10010  22 18 12
 10011  23 19 13
 10100  24 20 14
 10101  25 21 15
 10110  26 22 16
 10111  27 23 17
 11000  30 24 18
 11001  31 25 19
 11010  32 26 1A
 11011  33 27 1B
 11100  34 28 1C
 11101  35 29 1D
 11110  36 30 1E
 11111  37 31 1F
100000  40 32 20
100001  41 33 21

>>> </python>