Non-decimal radices/Output: Difference between revisions

From Rosetta Code
Content added Content deleted
(added common lisp)
m (→‎{{header|Java}}: Added loop to be more like other examples)
Line 59: Line 59:
=={{header|Java}}==
=={{header|Java}}==
<lang java5>public static void main(String args[]){
<lang java5>public static void main(String args[]){
int a;
for(int a= 0;a < 33;a++){
System.out.println(Integer.toBinaryString(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.toOctalString(a));
System.out.println(Integer.toHexString(a));
//the above methods treat the integer as unsigned
System.out.println(Integer.toHexString(a));
//there are also corresponding Long.to***String() methods for long's.
// the above methods treat the integer as unsigned
// there are also corresponding Long.to***String() methods for long's.


System.out.printf("%3o %2d %2x\n"); // printf like the other languages; binary not supported
System.out.printf("%3o %2d %2x\n",a ,a ,a); //printf like the other languages; binary not supported
}
}</lang>
}</lang>



Revision as of 03:36, 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>

  1. 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.

Common Lisp

<lisp>(loop for n from 0 to 33 do

 (format t " ~6B ~3O ~2D ~2X~%" n n n n))</lisp>

Fortran

Works with: Fortran version 90 and later

<fortran> do n = 1, 33

 write(*, "(b6, o4, i4, z4)") n, n, n, n

end do </fortran>

Haskell

<haskell>import Text.Printf

main :: IO () main = mapM_ f [0..33] where

 f :: Int -> IO ()
 f n = printf " %3o %2d %2X\n" n n n -- binary not supported</haskell>

Java

<lang java5>public static void main(String args[]){

  for(int a= 0;a < 33;a++){
     System.out.println(Integer.toBinaryString(a));
     System.out.println(Integer.toOctalString(a));
     System.out.println(Integer.toHexString(a));
     //the above methods treat the integer as unsigned
     //there are also corresponding Long.to***String() methods for long's.
     System.out.printf("%3o %2d %2x\n",a ,a ,a); //printf like the other languages; binary not supported
  }

}</lang>

OCaml

<ocaml>for n = 0 to 33 do

 Printf.printf " %3o %2d %2X\n" n n n (* binary not supported *)

done</ocaml>

Perl

<perl>foreach my $n (0..33) {

 printf " %6b %3o %2d %2X\n", $n, $n, $n, $n;

}</perl>

PHP

<php><?php foreach (range(0, 33) as $n) {

 printf(" %6b %3o %2d %2X\n", $n, $n, $n, $n);

} ?></php>

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) #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>
Works with: Python version 2.5

Octal (o), Decimal (d), and Hexadecimal (X and x) are supported by the printf format (binary not supported): <python>for n in range(34): print " %3o %2d %2X" % (n, n, n)</python>


For each of these bases there is also a built-in function that will convert it to a string with the proper prefix appended, so that it is a valid Python expression: <python>n = 33

  1. Python 3.x:

print(bin(n), oct(n), n, hex(n)) # bin() only available in Python 3.x and 2.6

  1. output: 0b100001 0o41 33 0x21
  1. Python 2.x:
  2. print oct(n), n, hex(n)
  3. output: 041 33 0x21</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