Non-decimal radices/Output: Difference between revisions

From Rosetta Code
Content added Content deleted
(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...)
 
(added ruby)
Line 4: Line 4:


=={{header|Python}}==
=={{header|Python}}==
{{works with|Python|2.6}}
Binary (b), Octal (o), Decimal (d), and Hexadecimal (X and x) are supported by the [http://www.python.org/dev/peps/pep-3101/ format]method of a string
Binary (b), Octal (o), Decimal (d), and Hexadecimal (X and x) are supported by the [http://www.python.org/dev/peps/pep-3101/ format]method of a string
<python>>>> for n in range(34):
<python>>>> for n in range(34):
Line 44: Line 45:
100001 41 33 21
100001 41 33 21
>>> </python>
>>> </python>

=={{header|Ruby}}==
<ruby>irb(main):001:0> for n in 0..33
irb(main):002:1> puts " %6b %3o %2d %2X" % [n, n, n, n]
irb(main):003:1> end
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
=> 0..33</ruby>

Revision as of 07:44, 31 January 2009

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

Works with: Python version 2.6

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>

Ruby

irb(main):001:0> for n in 0..33 irb(main):002:1> puts " %6b %3o %2d %2X" % [n, n, n, n] irb(main):003:1> end

     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

=> 0..33