Non-decimal radices/Output: Difference between revisions

m
m (→‎{{header|Wren}}: Library name change.)
m (→‎{{header|Wren}}: Minor tidy)
 
(14 intermediate revisions by 13 users not shown)
Line 13:
The reverse operation is [[Common number base parsing]].
<br><br>
 
=={{header|11l}}==
<syntaxhighlight lang="11l">V n = 33
print(bin(n)‘ ’String(n, radix' 8)‘ ’n‘ ’hex(n))</syntaxhighlight>
 
{{out}}
<pre>
100001 41 33 21
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">INCLUDE "D2:PRINTF.ACT" ;from the Action! Tool Kit
 
PROC Main()
CARD ARRAY v=[6502 1977 2021 256 1024 12345 9876 1111 0 16]
BYTE i,LMARGIN=$52,old
 
old=LMARGIN
LMARGIN=0 ;remove left margin on the screen
Put(125) PutE() ;clear the screen
 
FOR i=0 TO 9
DO
PrintF("(dec) %D = (hex) %H = (oct) %O%E",v(i),v(i),v(i))
OD
 
LMARGIN=old ;restore left margin on the screen
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Non-decimal_radices_output.png Screenshot from Atari 8-bit computer]
<pre>
(dec) 6502 = (hex) 1966 = (oct) 14546
(dec) 1977 = (hex) 7B9 = (oct) 3671
(dec) 2021 = (hex) 7E5 = (oct) 3745
(dec) 256 = (hex) 100 = (oct) 400
(dec) 1024 = (hex) 400 = (oct) 2000
(dec) 12345 = (hex) 3039 = (oct) 30071
(dec) 9876 = (hex) 2694 = (oct) 23224
(dec) 1111 = (hex) 457 = (oct) 2127
(dec) 0 = (hex) 0 = (oct) 0
(dec) 16 = (hex) 10 = (oct) 20
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
 
Line 26 ⟶ 69:
New_Line;
end loop;
end Test_Integer_Text_IO;</langsyntaxhighlight>
Sample output:
<pre style="height:30ex;overflow:scroll">
Line 65 ⟶ 108:
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">o_xinteger(16, 1000000);
o_byte('\n');
o_xinteger(5, 1000000);
o_byte('\n');
o_xinteger(2, 1000000);
o_byte('\n');</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 79 ⟶ 122:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - printf has been removed}}
<langsyntaxhighlight lang="algol68">main:(
FOR i TO 33 DO
printf(($10r6d," "16r6d," "8r6dl$, BIN i, BIN i, BIN i))
OD
)</langsyntaxhighlight>
Sample output:
<pre>
Line 123 ⟶ 166:
=={{header|ALGOL W}}==
Algol W has a standard procedure intbase16 that returns its parameter converted to a string in hexadecimal.
<langsyntaxhighlight lang="algolw">begin
% print some numbers in hex %
for i := 0 until 20 do write( intbase16( i ) )
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 154 ⟶ 197:
=={{header|AutoHotkey}}==
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276235.html#276235 forum]
<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox % BC("FF",16,3) ; -> 100110 in base 3 = FF in hex = 256 in base 10
 
BC(NumStr,InputBase=8,OutputBase=10) {
Line 160 ⟶ 203:
DllCall("msvcrt\_i64toa","Int64",DllCall("msvcrt\_strtoui64","Str",NumStr,"Uint",0,"UInt",InputBase,"CDECLInt64"),"Str",S,"UInt",OutputBase,"CDECL")
Return S
}</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">loop 0..33 'i ->
print [
pad as.binary i 6
pad as.octal i 2
pad to :string i 2
pad as.hex i 2
]</syntaxhighlight>
 
{{out}}
 
<pre> 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</pre>
 
=={{header|AWK}}==
C's printf() is just exposed:
<langsyntaxhighlight lang="awk">$ awk '{printf("%d 0%o 0x%x\n",$1,$1,$1)}'
10
10 012 0xa
Line 170 ⟶ 260:
16 020 0x10
255
255 0377 0xff</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> REM STR$ converts to a decimal string:
PRINT STR$(0)
PRINT STR$(123456789)
Line 180 ⟶ 270:
REM STR$~ converts to a hexadecimal string:
PRINT STR$~(43981)
PRINT STR$~(-1)</langsyntaxhighlight>
'''Output:'''
<pre>
Line 193 ⟶ 283:
Variable <code>obase</code> is the base for all output. It can be 2 (binary) up to some implementation-dependent limit. In [[GNU bc]] the limit may be large, for example 2^31, with "digits" of bases bigger than 36 printed as individual decimal numbers.
{{works with|GNU bc}}
<syntaxhighlight lang="bc">
<lang Bc>
for(i=1;i<10;i++) {
obase=10; print i," "
Line 200 ⟶ 290:
obase=2; print i
print "\n"
}</langsyntaxhighlight>
{{out}}
<pre>1 1 1 1
Line 214 ⟶ 304:
=={{header|C}}==
 
<langsyntaxhighlight lang="c">#include <stdio.h>
 
int main()
Line 224 ⟶ 314:
 
return 0;
}</langsyntaxhighlight>
 
Binary conversion using <tt>%b</tt> is not standard.
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">
using System;
 
Line 250 ⟶ 340:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 301 ⟶ 391:
=={{header|C++}}==
 
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <iomanip>
 
Line 312 ⟶ 402:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
Clojure eschews duplicating functionality already present in Java when interop is sufficiently idiomatic:
<langsyntaxhighlight lang="lisp">(Integer/toBinaryString 25) ; returns "11001"
(Integer/toOctalString 25) ; returns "31"
(Integer/toHexString 25) ; returns "19"
 
(dotimes [i 20]
(println (Integer/toHexString i)))</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(loop for n from 0 to 33 do
(format t " ~6B ~3O ~2D ~2X~%" n n n n))</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio;
 
void main() {
Line 335 ⟶ 425:
foreach (i; 0 .. 34)
writefln(" %6b %6o %6d %6x", i, i, i, i);
}</langsyntaxhighlight>
{{out}}
<pre>Base: 2 8 10 16
Line 378 ⟶ 468:
 
{{libheader|Tango}}
<langsyntaxhighlight lang="d">for (int i = 0; i < 35; i++)
Stdout.formatln ("{:b8} {:o3} {} {:x2}", i, i, i, i);</langsyntaxhighlight>
 
=={{header|Dc}}==
<langsyntaxhighlight Dclang="dc">[ dn [ ]P ]sp
[
2o lpx
Line 392 ⟶ 482:
1+ d21>b
]sb
1 lbx</langsyntaxhighlight>
Bases above 16 print blank separated "digits" (in decimal)
{{out}}
Line 415 ⟶ 505:
10011 23 19 13 01 02
10100 24 20 14 01 03</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Delphi has native support for decimal and hexadecimal output. There is much broader support for floating point output.
 
<syntaxhighlight lang="Delphi">
 
procedure ShowRadixOutput(Memo: TMemo);
var I: integer;
begin
I:=123456789;
Memo.Lines.Add('Decimal Hexadecimal');
Memo.Lines.Add('-----------------------');
Memo.Lines.Add(IntToStr(I)+' - '+IntToHex(I,8));
end;
 
</syntaxhighlight>
{{out}}
<pre>
Decimal Hexadecimal
-----------------------
123456789 - 075BCD15
 
</pre>
 
 
=={{header|E}}==
<langsyntaxhighlight lang="e">for value in 0..33 {
for base in [2, 8, 10, 12, 16, 36] {
def s := value.toString(base)
Line 423 ⟶ 539:
}
println()
}</langsyntaxhighlight>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">Enum.each(0..32, fn i -> :io.format "~2w :~6.2B, ~2.8B, ~2.16B~n", [i,i,i,i] end)</langsyntaxhighlight>
 
{{out}}
Line 474 ⟶ 590:
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">for i = 1 to 33 do
printf(1,"%6d %6x %6o\n",{i,i,i})
end for</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<p>Base 8, 10 and 16 can be output by <code>printf</code></p>
<langsyntaxhighlight lang="fsharp">let ns = [30..33]
ns |> Seq.iter (fun n -> printfn " %3o %2d %2X" n n n)</langsyntaxhighlight>
{{out}}
<pre> 36 30 1E
Line 488 ⟶ 604:
41 33 21</pre>
<p>The .NET library <code>System.Convert</code> is able to also convert from and to base 2</p>
<langsyntaxhighlight lang="fsharp">let bases = [2; 8; 10; 16]
 
ns |> Seq.map (fun n -> Seq.initInfinite (fun i -> n))
|> Seq.map (fun s -> Seq.zip s bases)
|> Seq.map (Seq.map System.Convert.ToString >> Seq.toList)
|> Seq.iter (fun s -> (printfn "%6s %2s %2s %2s" s.[0] s.[1] s.[2] s.[3]))</langsyntaxhighlight>
{{out}}
<pre> 11110 36 30 1e
Line 501 ⟶ 617:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">1234567 2 36 [a,b] [ >base print ] with each</langsyntaxhighlight>
<pre style="height:30ex;overflow:scroll">
100101101011010000111
Line 543 ⟶ 659:
{{works with|GNU Forth}}
GNU Forth has convenience functions for printing an integer in decimal or hex, regardless of the current BASE.
<langsyntaxhighlight lang="forth">: main 34 1 do cr i dec. i hex. loop ;
main
...
11 $B
...</langsyntaxhighlight>
This is not standardized because such functions are very easy to define as needed:
<langsyntaxhighlight lang="forth">: base. ( n base -- ) base @ >r base ! . r> base ! ;
: oct. ( n -- ) 8 base. ;
: bin. ( n -- ) 2 base. ;</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">do n = 1, 33
write(*, "(b6, o4, i4, z4)") n, n, n, n
end do</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
FreeBASIC has built in functions called Hex, Str, Oct and Bin which convert decimal numbers into hexadecimal, decimal,
octal and binary strings respectively. Here's an example:
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Dim ui(1 To 4) As UInteger = {10, 26, 52, 100}
Line 571 ⟶ 687:
Next
 
Sleep</langsyntaxhighlight>
 
{{out}}
Line 585 ⟶ 701:
=={{header|Gema}}==
After decimal numbers in the input stream, add hexadecimal and octal of the same number in the output stream. Also after hexadecimal add decimal and octal, and after octal add decimal and hexadecimal.
<langsyntaxhighlight lang="gema">0x<A>=$0 (@radix{16;10;$1}, 0@radix{16;8;$1})
0<D>=$0 (@radix{8;10;$1}, 0x@radix{8;16;$1})
<D>=$0 (0x@radix{10;16;$1}, 0@radix{10;8;$1})</langsyntaxhighlight>
Invocation and sample input and output
<pre>$ gema -p radix.gema
Line 594 ⟶ 710:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 618 ⟶ 734:
// There no equivalent for big ints.
fmt.Println(strconv.FormatInt(1313, 19))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 633 ⟶ 749:
 
=={{header|Haskell}}==
<langsyntaxhighlight 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</langsyntaxhighlight>
 
alternately, without <code>Text.Printf</code>:
<langsyntaxhighlight lang="haskell">import Numeric
 
main :: IO ()
main = mapM_ f [0..33] where
f :: Int -> IO ()
f n = putStrLn $ " " ++ showOct n "" ++ " " ++ show n ++ " " ++ showHex n ""</langsyntaxhighlight>
 
Or, generalising and tabulating a little:
<langsyntaxhighlight lang="haskell">import Data.List (unfoldr, transpose, intercalate)
import Data.Array (Array, listArray, (!))
import Data.Monoid ((<>))
Line 688 ⟶ 804:
mapM_
putStrLn
(table " " (([fmap show, fmap $ const "----"] <*> [bases]) <> tableRows))</langsyntaxhighlight>
{{Out}}
<pre> 2 7 8 10 12 16 32
Line 727 ⟶ 843:
 
=={{header|HicEst}}==
<langsyntaxhighlight HicEstlang="hicest">DO n = 1, 33
WRITE(Format="b6.0, o4.0, i4.0, z4.0") n, n, n, n
ENDDO</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Strictly speaking output conversion to different representations isn't built-in to Icon and Unicon; however, printf is included as part of the standard library.
<langsyntaxhighlight Iconlang="icon">procedure main()
write("Non-decimal radices/Output")
every i := 255 | 2 | 5 | 16 do {
Line 742 ⟶ 858:
printf("%%i = %i\n",i) # image format
}
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 759 ⟶ 875:
J can natively break out numbers using a specific base
 
<langsyntaxhighlight lang="j"> 2 #.inv 12
1 1 0 0
3 #.inv 100
1 0 2 0 1
16 #.inv 180097588
10 11 12 1 2 3 4</langsyntaxhighlight>
However, this numeric representation would not satisfy most people's idea of "formatting", for most bases. It might be useful, however, for bases less than 10:
 
<langsyntaxhighlight lang="j"> 8 #.inv 4009
7 6 5 1
-.&' '": 8 #.inv 4009
7651</langsyntaxhighlight>
J also includes some explicit support for hexadecimal numbers
 
<langsyntaxhighlight lang="j"> require 'convert'
hfd 180097588
ABC1234</langsyntaxhighlight>
(and a few other hexadecimal related mechanisms which are not relevant here.)
 
=={{header|Java}}==
<langsyntaxhighlight lang="java5">public static void main(String args[]){
for(int a= 0;a < 33;a++){
System.out.println(Integer.toBinaryString(a));
Line 789 ⟶ 905:
System.out.printf("%3o %2d %2x\n",a ,a ,a); //printf like the other languages; binary not supported
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
The <code><i>number</i>.toString(<i>radix</i>)</code> method produces a string representation of a number in any radix between 2 and 36.
 
<langsyntaxhighlight lang="javascript">var bases = [2, 8, 10, 16, 24];
for (var n = 0; n <= 33; n++) {
var row = [];
Line 800 ⟶ 916:
row.push( n.toString(bases[i]) );
print(row.join(', '));
}</langsyntaxhighlight>
 
outputs
Line 839 ⟶ 955:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes, Printf
 
println("Primes ≤ $hi written in common bases.")
Line 846 ⟶ 962:
@printf("%8s%8s%8s%8s\n", bin(i), oct(i), dec(i), hex(i))
end
</langsyntaxhighlight>{{out}}
<pre>
Primes ≤ 50 written in common bases.
Line 866 ⟶ 982:
101111 57 47 2f
</pre>
 
=={{header|Klingphix}}==
<syntaxhighlight lang="klingphix">include ..\Utilitys.tlhy
 
33 [
( "decimal: " swap " bin: " over 8 itob reverse ) lprint nl
] for
 
"End " input</syntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
fun main(args: Array<String>) {
Line 879 ⟶ 1,004:
println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 925 ⟶ 1,050:
=={{header|Locomotive Basic}}==
 
<langsyntaxhighlight lang="locobasic">10 FOR i=1 TO 20
20 PRINT i,BIN$(i),HEX$(i)
30 NEXT</langsyntaxhighlight>
 
Output:
Line 953 ⟶ 1,078:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">for i = 1, 33 do
print( string.format( "%o \t %d \t %x", i, i, i ) )
end</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang Mathematica="mathematica">Scan[PrintTable[IntegerString[#n, 2b], "{n,"Range@38}, IntegerString[#{b,{2, 8,16,36}}], // Grid</syntaxhighlight>
{{out}}
",",#, ",",IntegerString[#, 16],",", IntegerString[#, 36]]&, Range[38]]</lang>
<pre>1 1 1 1
 
10 2 2 2
Output:
 
11 3 3 3
<pre>
 
1,1,1,1,1
100 4 4 4
10,2,2,2,2
...
11,3,3,3,3
 
...
100010 42 22 y
...
 
100010,42,34,22,y
100011, 43,35, 23, z
 
100100,44,36,24,10
100100 44 24 10
100101,45,37,25,11
 
100110,46,38,26,12</pre>
100101 45 25 11
 
100110 46 26 12
</pre>
 
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight MATLABlang="matlab">fprintf('%3d %3o %3x\n',repmat(1:20,3,1))</langsyntaxhighlight>
Output:
<pre> 1 1 1
Line 1,000 ⟶ 1,130:
 
=={{header|Modula-3}}==
<langsyntaxhighlight lang="modula3">MODULE Conv EXPORTS Main;
 
IMPORT IO, Fmt;
Line 1,011 ⟶ 1,141:
IO.Put("\n");
END;
END Conv.</langsyntaxhighlight>
Output:
<pre style="height:30ex;overflow:scroll">
Line 1,050 ⟶ 1,180:
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 1,088 ⟶ 1,218:
fm.format("[Base 16=%1$8x,Base 10=%1$8d,Base 8=%1$8o,Base 2=%2$20s]", [Object Long(n_), String('_')])
return fb.toString()
</syntaxhighlight>
</lang>
'''Output:'''
<pre style="height:30ex; overflow:scroll;">
Line 1,126 ⟶ 1,256:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import strutils
 
for i in 0..33:
echo toBin(i, 6)," ",toOct(i, 3)," ",align($i,2)," ",toHex(i,2)</langsyntaxhighlight>
Output:
<pre>000000 000 0 00
Line 1,167 ⟶ 1,297:
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">for n = 0 to 33 do
Printf.printf " %3o %2d %2X\n" n n n (* binary not supported *)
done</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
The only bases supported by the language itself (as opposed to custom functions) are binary and decimal.
<langsyntaxhighlight lang="parigp">printbinary(n)={
n=binary(n);
for(i=1,#n,print1(n[i]))
Line 1,179 ⟶ 1,309:
printdecimal(n)={
print1(n)
};</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">foreach my $n (0..33) {
printf " %6b %3o %2d %2X\n", $n, $n, $n, $n;
}</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang phix>for i=2 to 32 by 10 do
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
printf(1,"decimal:%3d hex:%3x octal:%3o binary:%7b\n",i)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">32</span> <span style="color: #008080;">by</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
end for</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"decimal:%3d hex:%3x octal:%3o binary:%7b\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,199 ⟶ 1,332:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight Phixmontilang="phixmonti">33 for
dup "decimal: " print print " bin: " print 8 int>bit print nl
endfor</langsyntaxhighlight>
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
foreach (range(0, 33) as $n) {
echo decbin($n), "\t", decoct($n), "\t", $n, "\t", dechex($n), "\n";
}
?></langsyntaxhighlight>
 
<langsyntaxhighlight lang="php"><?php
foreach (range(0, 33) as $n) {
printf(" %6b %3o %2d %2X\n", $n, $n, $n, $n);
}
?></langsyntaxhighlight>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de printNumber (N Base)
(when (>= N Base)
(printNumber (/ N Base) Base) )
Line 1,227 ⟶ 1,360:
(prinl)
(printNumber 123456789012345678901234567890 36))
(prinl)</langsyntaxhighlight>
Output:
<pre>1a
Line 1,233 ⟶ 1,366:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
get list (n);
put skip list (n); /* Prints N in decimal */
put skip edit (n) (B); /* prints N as a bit string, N > 0 */
</syntaxhighlight>
</lang>
 
=={{header|PowerShell}}==
The .NET class <code>Convert</code> handles conversions in binary, octal, decimal and hexadecimal. Furthermore, format strings may be used for hexadecimal conversion.
<langsyntaxhighlight lang="powershell">foreach ($n in 0..33) {
"Base 2: " + [Convert]::ToString($n, 2)
"Base 8: " + [Convert]::ToString($n, 8)
Line 1,249 ⟶ 1,382:
"Base 16: " + [Convert]::ToString($n, 16)
"Base 16: " + ("{0:X}" -f $n)
}</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">For i=105 To 115
Bin$=RSet(Bin(i),8,"0") ;- Convert to wanted type & pad with '0'
Hex$=RSet(Hex(i),4,"0")
Dec$=RSet(Str(i),3)
PrintN(Dec$+" decimal = %"+Bin$+" = $"+Hex$+".")
Next</langsyntaxhighlight>
 
105 decimal = %01101001 = $0069.
Line 1,274 ⟶ 1,407:
{{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
<div style="height:30ex;overflow:scroll"><langsyntaxhighlight 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
Line 1,315 ⟶ 1,448:
100000 40 32 20
100001 41 33 21
>>></langsyntaxhighlight></div>
 
{{works with|Python|2.5}}
Octal (o), Decimal (d), and Hexadecimal (X and x), but not binary are supported by the string modulo operator, %:
<langsyntaxhighlight lang="python">for n in range(34):
print " %3o %2d %2X" % (n, n, n)</langsyntaxhighlight>
 
----
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:
<langsyntaxhighlight lang="python">n = 33
#Python 3.x:
print(bin(n), oct(n), n, hex(n)) # bin() only available in Python 3.x and 2.6
Line 1,331 ⟶ 1,464:
#Python 2.x:
#print oct(n), n, hex(n)
# output: 041 33 0x21</langsyntaxhighlight>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> ' [ 22 333 4444 55555 ] witheach
[ dup
say "Decimal " echo cr
dup
' [ 2 3 4 5 ] witheach
[ 2dup say " in base " echo
swap base put
say " -> " echo cr
base release ]
cr 2drop ]</syntaxhighlight>
 
{{out}}
 
<pre>Decimal 22
in base 2 -> 10110
in base 3 -> 211
in base 4 -> 112
in base 5 -> 42
 
Decimal 333
in base 2 -> 101001101
in base 3 -> 110100
in base 4 -> 11031
in base 5 -> 2313
 
Decimal 4444
in base 2 -> 1000101011100
in base 3 -> 20002121
in base 4 -> 1011130
in base 5 -> 120234
 
Decimal 55555
in base 2 -> 1101100100000011
in base 3 -> 2211012121
in base 4 -> 31210003
in base 5 -> 3234210
</pre>
 
=={{header|R}}==
Conversion to and from binary does not have built-in support.
<langsyntaxhighlight Rlang="r"># dec to oct
as.octmode(x)
# dec to hex
Line 1,342 ⟶ 1,515:
as.integer(x)
# or
as.numeric(x)</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 1,359 ⟶ 1,532:
;; "3a" "69" "b8" "38" "7b" "47" "f6" "96" "36" "i5" "d5" "85" "35"
;; "n4" "j4" "f4" "b4" "74" "34" "u3" "r3" "o3" "l3" "i3" "f3")
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 1,366 ⟶ 1,539:
Calling the <code>.base</code> method on a number returns a string. It can handle all bases between 2 and 36:
 
<syntaxhighlight lang="raku" perl6line>say 30.base(2); # "11110"
say 30.base(8); # "36"
say 30.base(10); # "30"
say 30.base(16); # "1E"
say 30.base(30); # "10"</langsyntaxhighlight>
 
Alternatively, <code>printf</code> can be used for some common number bases:
<syntaxhighlight lang="raku" perl6line>for 0..33 -> $n {
printf " %6b %3o %2d %2X\n", $n xx 4;
}</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 1,383 ⟶ 1,556:
<br><br>The reason for the apparent complexity of the '''D2B''' function is to handle the special case of
<br>zero &nbsp; (with regards to striping leading zeroes from the converted number)..
<langsyntaxhighlight lang="rexx">/*REXX pgm shows REXX's ability to show decimal numbers in binary & hex.*/
 
do j=0 to 50 /*show some low-value num conversions*/
Line 1,392 ⟶ 1,565:
exit /*stick a fork in it, we're done.*/
/*────────────────────────────D2B subroutine────────────────────────────*/
d2b: return word(strip(x2b(d2x(arg(1))),'L',0) 0,1) /*convert dec──►bin*/</langsyntaxhighlight>
'''output'''
<pre style="height:20ex">
Line 1,452 ⟶ 1,625:
<br><br>Of course, using base 256 is hampered in ASCII machines in that some lower values are
<br>interpreted by the operating system as control characters and therefore aren't displayed as their (true) glyph.
<langsyntaxhighlight lang="rexx">/*REXX program shows REXX's ability to show dec nums in bin/hex/base256.*/
 
do j=14 to 67 /*display some lower-value numbers. */
Line 1,462 ⟶ 1,635:
exit /*stick a fork in it, we're done.*/
/*────────────────────────────D2B subroutine────────────────────────────*/
d2b: return word(strip(x2b(d2x(arg(1))),'L',0) 0,1) /*convert dec──►bin*/</langsyntaxhighlight>
'''output'''
<pre style="height:20ex">
Line 1,522 ⟶ 1,695:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Non Decimal radices/Output
 
Line 1,531 ⟶ 1,704:
see upper(hex(43981)) + nl
see upper(hex(-1)) + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,539 ⟶ 1,712:
ABCD
FFFFFFFF
</pre>
 
=={{header|RPL}}==
Unsigned integers are displayed in binary, octal, decimal or hexadecimal base depending on the state of 2 user flags, which can be easily configured by using resp. the <code>BIN</code>, <code>OCT</code>, <code>DEC</code> or <code>HEX</code> instruction. It is not possible to display several numbers in different bases simultaneously, unless you "freeze" their appearance by converting them to a string:
#314 DUP BIN →STR " " +
OVER OCT →STR + " " +
OVER DEC →STR + " " +
OVER HEX →STR +
{{out}}
<pre>
1: "# 100111010b # 472o # 314d # 13Ah"
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">for n in 0..33
puts " %6b %3o %2d %2X" % [n, n, n, n]
end
puts
[2,8,10,16,36].each {|i| puts " 100.to_s(#{i}) => #{100.to_s(i)}"}</langsyntaxhighlight>
{{out}}
<div style="height:30ex;overflow:scroll">
Line 1,590 ⟶ 1,774:
100.to_s(36) => 2s
</div>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">fn main() {
// To render the number as string, use format! macro instead
println!("Binary: {:b}", 0xdeadbeefu32);
println!("Binary with 0b prefix: {:#b}", 0xdeadbeefu32);
println!("Octal: {:o}", 0xdeadbeefu32);
println!("Octal with 0o prefix: {:#o}", 0xdeadbeefu32);
println!("Decimal: {}", 0xdeadbeefu32);
println!("Lowercase hexadecimal: {:x}", 0xdeadbeefu32);
println!("Lowercase hexadecimal with 0x prefix: {:#x}", 0xdeadbeefu32);
println!("Uppercase hexadecimal: {:X}", 0xdeadbeefu32);
println!("Uppercase hexadecimal with 0x prefix: {:#X}", 0xdeadbeefu32);
}</syntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">
print asc("X") ' convert to ascii
print chr$(169) ' ascii to character
print dechex$(255) ' decimal to hex
print hexdec("FF") ' hex to decimal
print str$(467) ' decimal to string
print val("27") ' string to decimal
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object Main extends App {
val radices = List(2, 8, 10, 16, 19, 36)
for (base <- radices) print(f"$base%6d")
Line 1,610 ⟶ 1,808:
eol = if (radix == radices.last) '\n' else '\0'
) print(f"${i.toString(radix)}%6s$eol")
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(do ((i 0 (+ i 1)))
((>= i 33))
(display (number->string i 2)) ; binary
Line 1,622 ⟶ 1,820:
(display " ")
(display (number->string i 16)) ; hex
(newline))</langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 1,633 ⟶ 1,831:
The [http://seed7.sourceforge.net/libraries/string.htm#%28in_string%29lpad%28in_integer%29 lpad] operator
is used to pad the result of the ''radix'' operator at the left side. The padding is done with spaces.
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
Line 1,644 ⟶ 1,842:
i radix 16 lpad 6);
end for;
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">range(0, 33).each { |n|
printf(" %6b %3o %2d %2X\n", ([n]*4)...);
}</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
The radix can be from 2 to 49 and its value is prepended to the string followed by "r".
<langsyntaxhighlight lang="smalltalk">1 to: 33 do: [ :i |
('%1 %2 %3' % { i printStringRadix: 8. i printStringRadix: 16. i printStringRadix: 2 })
printNl.
].</langsyntaxhighlight>
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">let
fun loop i =
if i < 34 then (
Line 1,671 ⟶ 1,868:
in
loop 0
end</langsyntaxhighlight>
 
=={{header|Tcl}}==
 
The <code>format</code> command supports conversions to octal, decimal, and hex:
<langsyntaxhighlight lang="tcl">for {set n 0} {$n <= 33} {incr n} {
puts [format " %3o %2d %2X" $n $n $n]
}</langsyntaxhighlight>
<!--The following should be moved to: [[Number base conversion]]
 
Conversion to binary requires a procedure. Here's two ways to do it:
 
<langsyntaxhighlight lang="tcl"># process the value as if it's a string
proc int2bits {i} {
string map {0 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111} [format %o $i]
Line 1,692 ⟶ 1,889:
binary scan [binary format I1 $i] B* x
return $x
}</langsyntaxhighlight>
 
-->
Line 1,700 ⟶ 1,897:
Bases 2, 10, and 16 are supported. The base is controlled by a global ''mode''.
 
<langsyntaxhighlight lang="ti89b">Local old
getMode("Base")→old
setMode("Base", "BIN")
Line 1,708 ⟶ 1,905:
setMode("Base", "DEC")
Disp string(16)
setMode("Base", old)</langsyntaxhighlight>
 
Output:
 
<langsyntaxhighlight lang="ti89b">0b10000
0h10
16</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
Wren has no non-decimal number conversions in its standard library so this uses a module I wrote myself to reproduce the Haskell table.
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Conv, Fmt
 
System.print(" 2 7 8 10 12 16 32")
Line 1,732 ⟶ 1,929:
var b32 = Fmt.s(4, Conv.Itoa(i, 32))
System.print("%(b2) %(b7) %(b8) %(b10) %(b12) %(b16) %(b32)")
}</langsyntaxhighlight>
 
{{out}}
Line 1,774 ⟶ 1,971:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
int N;
[N:= 2;
Line 1,781 ⟶ 1,978:
N:= N*N;
until N=0;
]</langsyntaxhighlight>
 
Output:
Line 1,793 ⟶ 1,990:
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">for i = 1 to 33
print "decimal: ", i, " hex: ", hex$(i), " bin: ", bin$(i)
next
</syntaxhighlight>
</lang>
 
=={{header|zkl}}==
"%.nB" formats a [unsigned] number in base n (2-36). So
<langsyntaxhighlight lang="zkl">const N=16;
var fmt=[2..N].pump(String,"%%5.%dB".fmt); // %5.2B%5.3B%5.4B%5.5B ...
foreach n in (17){fmt.fmt(n.pump(N,List,n.fp(n)).xplode()).println()}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,823 ⟶ 2,020:
10000 121 100 31 24 22 20 17 16 15 14 13 12 11 10
</pre>
<langsyntaxhighlight lang="zkl">(100).toString(36) //-->"2s"</langsyntaxhighlight>
For binary, decimal and hex, you can also have [fixed, sorry Europe] separators:
<langsyntaxhighlight lang="zkl">"%,.2B".fmt(1234567) //-->"1|0010|1101|0110|1000|0111"
"%,d".fmt(1234567) //-->"1,234,567"
"%,x".fmt(1234567) //-->"12|d6|87"</langsyntaxhighlight>
 
 
9,476

edits