Non-decimal radices/Output

From Rosetta Code
Revision as of 05:13, 20 June 2009 by rosettacode>Tinku99 (+ AutoHotkey)
Task
Non-decimal radices/Output
You are encouraged to solve this task according to the task description, using any language you may know.

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

The reverse operation is parsing numbers in common bases.

Ada

<lang ada> with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; with Ada.Text_IO; use Ada.Text_IO;

procedure Test_Integer_Text_IO is begin

 for I in 1..33 loop
   Put (I, Width =>3, Base=> 10);
   Put (I, Width =>7, Base=> 16);
   Put (I, Width =>6, Base=>  8);
   New_Line;
 end loop;

end Test_Integer_Text_IO; </lang> Sample output:

  1  16#1#  8#1#
  2  16#2#  8#2#
  3  16#3#  8#3#
  4  16#4#  8#4#
  5  16#5#  8#5#
  6  16#6#  8#6#
  7  16#7#  8#7#
  8  16#8# 8#10#
  9  16#9# 8#11#
 10  16#A# 8#12#
 11  16#B# 8#13#
 12  16#C# 8#14#
 13  16#D# 8#15#
 14  16#E# 8#16#
 15  16#F# 8#17#
 16 16#10# 8#20#
 17 16#11# 8#21#
 18 16#12# 8#22#
 19 16#13# 8#23#
 20 16#14# 8#24#
 21 16#15# 8#25#
 22 16#16# 8#26#
 23 16#17# 8#27#
 24 16#18# 8#30#
 25 16#19# 8#31#
 26 16#1A# 8#32#
 27 16#1B# 8#33#
 28 16#1C# 8#34#
 29 16#1D# 8#35#
 30 16#1E# 8#36#
 31 16#1F# 8#37#
 32 16#20# 8#40#
 33 16#21# 8#41#

ALGOL 68

Translation of: C
Works with: ALGOL 68 version Standard - no extensions to language used
Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386
main:(
  FOR i TO 33 DO
    printf(($10r6d," "16r6d," "8r6dl$, BIN i, BIN i, BIN i))
  OD
)

Sample output:

000001 000001 000001
000002 000002 000002
000003 000003 000003
000004 000004 000004
000005 000005 000005
000006 000006 000006
000007 000007 000007
000008 000008 000010
000009 000009 000011
000010 00000a 000012
000011 00000b 000013
000012 00000c 000014
000013 00000d 000015
000014 00000e 000016
000015 00000f 000017
000016 000010 000020
000017 000011 000021
000018 000012 000022
000019 000013 000023
000020 000014 000024
000021 000015 000025
000022 000016 000026
000023 000017 000027
000024 000018 000030
000025 000019 000031
000026 00001a 000032
000027 00001b 000033
000028 00001c 000034
000029 00001d 000035
000030 00001e 000036
000031 00001f 000037
000032 000020 000040
000033 000021 000041

AutoHotkey

contributed by Laszlo on the ahk forum <lang AutoHotkey>MsgBox % BC("FF",16,3) ; -> 100110 in base 3 = FF in hex = 256 in base 10

BC(NumStr,InputBase=8,OutputBase=10) {

 Static S = 12345678901234567890123456789012345678901234567890123456789012345
 DllCall("msvcrt\_i64toa","Int64",DllCall("msvcrt\_strtoui64","Str",NumStr,"Uint",0,"UInt",InputBase,"CDECLInt64"),"Str",S,"UInt",OutputBase,"CDECL")
 Return S

}</lang>

AWK

C's printf() is just exposed: <lang awk> $ awk '{printf("%d 0%o 0x%x\n",$1,$1,$1)}' 10 10 012 0xa 16 16 020 0x10 255 255 0377 0xff </lang>

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

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

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

D

Works with: Tango

Number following formatting character is width. When no formatting character is specified it is inferred from variable's type.

<lang d>

   for (int i = 0; i < 35; i++)
       Stdout.formatln ("{:b8} {:o3} {} {:x2}", i, i, i, i);

</lang>

E

<lang e>for value in 0..33 {

 for base in [2, 8, 10, 12, 16, 36] {
   def s := value.toString(base)
   print(" " * (8 - s.size()), s)
 }
 println()

}</lang>

Forth

Works with: GNU Forth

GNU Forth has convenience functions for printing an integer in decimal or hex, regardless of the current BASE. <lang forth>

: main 34 1 do cr i dec. i hex. loop ;
main
...
11 $B
...

</lang> This is not standardized because such functions are very easy to define as needed: <lang forth>

: base. ( n base -- ) base @ >r  base !  .  r> base ! ;
: oct. ( n -- ) 8 base. ;
: bin. ( n -- ) 2 base. ;

</lang>

Fortran

Works with: Fortran version 90 and later

<lang fortran> do n = 1, 33

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

end do </lang>

Haskell

<lang 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</lang>

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>

Modula-3

<lang modula3>MODULE Conv EXPORTS Main;

IMPORT IO, Fmt;

BEGIN

 FOR i := 1 TO 33 DO
   IO.Put(Fmt.Int(i, base := 10) & " ");
   IO.Put(Fmt.Int(i, base := 16) & " ");
   IO.Put(Fmt.Int(i, base := 8) & " ");
   IO.Put("\n");
 END;

END Conv. </lang> Output:

1 1 1 
2 2 2 
3 3 3 
4 4 4 
5 5 5 
6 6 6 
7 7 7 
8 8 10 
9 9 11 
10 a 12 
11 b 13 
12 c 14 
13 d 15 
14 e 16 
15 f 17 
16 10 20 
17 11 21 
18 12 22 
19 13 23 
20 14 24 
21 15 25 
22 16 26 
23 17 27 
24 18 30 
25 19 31 
26 1a 32 
27 1b 33 
28 1c 34 
29 1d 35 
30 1e 36 
31 1f 37 
32 20 40 
33 21 41

OCaml

<lang ocaml>for n = 0 to 33 do

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

done</lang>

Perl

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

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

}</lang>

PHP

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

 echo decbin($n), "\t", decoct($n), "\t", $n, "\t", dechex($n), "\n";

} ?></lang>

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

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

} ?></lang>

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

<lang 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
>>> </lang>
Works with: Python version 2.5

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


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: <lang 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</lang>

Ruby

<lang 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</lang>

Standard ML

<lang sml>let

 fun loop i =
   if i < 34 then (
     print (Int.fmt StringCvt.BIN i ^ "\t"
          ^ Int.fmt StringCvt.OCT i ^ "\t"
          ^ Int.fmt StringCvt.DEC i ^ "\t"
          ^ Int.fmt StringCvt.HEX i ^ "\n");
     loop (i+1)
   ) else ()

in

 loop 0

end</lang>

Tcl

The format command supports conversions to octal, decimal, and hex: <lang tcl>for {set n 0} {$n <= 33} {incr n} {

   puts [format " %3o %2d %2X" $n $n $n]

}</lang>