Non-decimal radices/Output: Difference between revisions

(added standard ml)
Line 377:
loop 0
end</lang>
 
=={{header|Tcl}}==
 
<code>format</code> command supports conversions to octal, decimal, hex:
<lang tcl>for {set n 0} {$n <= 33} {incr n} {
puts [format " %3o %2d %2X" $n $n $n]
}</lang>
 
Conversion to binary requires a procedure. Here's two ways to do it:
 
<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]
}
 
# format the number string as an integer, then scan into a binary string
proc int2bits {i} {
binary scan [binary format I1 $i] B* x
return $x
}</lang>
Anonymous user