Non-decimal radices/Output: Difference between revisions
→{{header|Python}}: Comment on Python 3.0 version |
Added Java |
||
Line 49: | Line 49: | ||
printf " %6b %3o %2d %2X\n", $n, $n, $n, $n; |
printf " %6b %3o %2d %2X\n", $n, $n, $n, $n; |
||
}</perl> |
}</perl> |
||
=={{header|Java}}== |
|||
<lang java5>public static void main(String args[]){ |
|||
int a; |
|||
//assign a to a value at some point |
|||
System.out.println(Integer.toBinaryString(a)); |
|||
System.out.println(Integer.toOctalString(a)); |
|||
System.out.println(Integer.toHexString(a)); |
|||
}</lang> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |
Revision as of 01:02, 1 February 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)
C
<lang c>#include <stdio.h>
int main() {
int i;
for(i=1; i <= 33; i++) printf("%6d %6x %6o\n", i, i, i);
return 0;
}</lang>
Binary conversion using %b is not standard.
C++
<lang cpp>#include <iostream>
- include <iomanip>
int main() {
for (int i = 0; i <= 33; i++) std::cout << std::setw(6) << std::dec << i << " " << std::setw(6) << std::hex << i << " " << std::setw(6) << std::oct << i << std::endl;
return 0;
}</lang>
Binary conversion is not standard.
Fortran
<fortran> do n = 1, 33
write(*, "(b6, o4, i4, z4)") n, n, n, n
end do </fortran>
Perl
<perl>foreach my $n (0..33) {
printf " %6b %3o %2d %2X\n", $n, $n, $n, $n;
}</perl>
Java
<lang java5>public static void main(String args[]){
int a; //assign a to a value at some point System.out.println(Integer.toBinaryString(a)); System.out.println(Integer.toOctalString(a)); System.out.println(Integer.toHexString(a));
}</lang>
PHP
<php><?php foreach (range(0, 33) as $n) {
printf(" %6b %3o %2d %2X\n", $n, $n, $n, $n);
} ?></php>
Python
Binary (b), Octal (o), Decimal (d), and Hexadecimal (X and x) are supported by the formatmethod of a string
print " {0:6b} {1:3o} {2:2d} {3:2X}".format(n, n, n, n) #The following would give the same output, and, #due to the outer brackets, works with Python 3.0 too #print ( " {n:6b} {n:3o} {n:2d} {n:2X}".format(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