Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Non-decimal-radices-Output/00-META.yaml
Normal file
3
Task/Non-decimal-radices-Output/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Non-decimal_radices/Output
|
||||
note: Arithmetic operations
|
||||
13
Task/Non-decimal-radices-Output/00-TASK.txt
Normal file
13
Task/Non-decimal-radices-Output/00-TASK.txt
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
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]].
|
||||
|
||||
|
||||
;Task:
|
||||
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 [[Common number base parsing]].
|
||||
<br><br>
|
||||
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
V n = 33
|
||||
print(bin(n)‘ ’String(n, radix' 8)‘ ’n‘ ’hex(n))
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
main:(
|
||||
FOR i TO 33 DO
|
||||
printf(($10r6d," "16r6d," "8r6dl$, BIN i, BIN i, BIN i))
|
||||
OD
|
||||
)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
begin
|
||||
% print some numbers in hex %
|
||||
for i := 0 until 20 do write( intbase16( i ) )
|
||||
end.
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
$ awk '{printf("%d 0%o 0x%x\n",$1,$1,$1)}'
|
||||
10
|
||||
10 012 0xa
|
||||
16
|
||||
16 020 0x10
|
||||
255
|
||||
255 0377 0xff
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
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;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
o_xinteger(16, 1000000);
|
||||
o_byte('\n');
|
||||
o_xinteger(5, 1000000);
|
||||
o_byte('\n');
|
||||
o_xinteger(2, 1000000);
|
||||
o_byte('\n');
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
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
|
||||
]
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
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
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
REM STR$ converts to a decimal string:
|
||||
PRINT STR$(0)
|
||||
PRINT STR$(123456789)
|
||||
PRINT STR$(-987654321)
|
||||
|
||||
REM STR$~ converts to a hexadecimal string:
|
||||
PRINT STR$~(43981)
|
||||
PRINT STR$~(-1)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
for(i=1;i<10;i++) {
|
||||
obase=10; print i," "
|
||||
obase=8; print i," "
|
||||
obase=3; print i," "
|
||||
obase=2; print i
|
||||
print "\n"
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
#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;
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
using System;
|
||||
|
||||
namespace NonDecimalRadicesOutput
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
for (int i = 0; i < 42; i++)
|
||||
{
|
||||
string binary = Convert.ToString(i, 2);
|
||||
string octal = Convert.ToString(i, 8);
|
||||
string hexadecimal = Convert.ToString(i, 16);
|
||||
Console.WriteLine(string.Format("Decimal: {0}, Binary: {1}, Octal: {2}, Hexadecimal: {3}", i, binary, octal, hexadecimal));
|
||||
}
|
||||
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i=1; i <= 33; i++)
|
||||
printf("%6d %6x %6o\n", i, i, i);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
(Integer/toBinaryString 25) ; returns "11001"
|
||||
(Integer/toOctalString 25) ; returns "31"
|
||||
(Integer/toHexString 25) ; returns "19"
|
||||
|
||||
(dotimes [i 20]
|
||||
(println (Integer/toHexString i)))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(loop for n from 0 to 33 do
|
||||
(format t " ~6B ~3O ~2D ~2X~%" n n n n))
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
import std.stdio;
|
||||
|
||||
void main() {
|
||||
writeln("Base: 2 8 10 16");
|
||||
writeln("----------------------------");
|
||||
foreach (i; 0 .. 34)
|
||||
writefln(" %6b %6o %6d %6x", i, i, i, i);
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
for (int i = 0; i < 35; i++)
|
||||
Stdout.formatln ("{:b8} {:o3} {} {:x2}", i, i, i, i);
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
[ dn [ ]P ]sp
|
||||
[
|
||||
2o lpx
|
||||
8o lpx
|
||||
10o lpx
|
||||
16o lpx
|
||||
17o lpx
|
||||
AP
|
||||
1+ d21>b
|
||||
]sb
|
||||
1 lbx
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
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;
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
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()
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
Enum.each(0..32, fn i -> :io.format "~2w :~6.2B, ~2.8B, ~2.16B~n", [i,i,i,i] end)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
for i = 1 to 33 do
|
||||
printf(1,"%6d %6x %6o\n",{i,i,i})
|
||||
end for
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
let ns = [30..33]
|
||||
ns |> Seq.iter (fun n -> printfn " %3o %2d %2X" n n n)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
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]))
|
||||
|
|
@ -0,0 +1 @@
|
|||
1234567 2 36 [a,b] [ >base print ] with each
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
: main 34 1 do cr i dec. i hex. loop ;
|
||||
main
|
||||
...
|
||||
11 $B
|
||||
...
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
: base. ( n base -- ) base @ >r base ! . r> base ! ;
|
||||
: oct. ( n -- ) 8 base. ;
|
||||
: bin. ( n -- ) 2 base. ;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
do n = 1, 33
|
||||
write(*, "(b6, o4, i4, z4)") n, n, n, n
|
||||
end do
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Dim ui(1 To 4) As UInteger = {10, 26, 52, 100}
|
||||
Print "Decimal Hex Octal Binary"
|
||||
Print "======= ======== ======= ======"
|
||||
For i As Integer = 1 To 4
|
||||
Print Str(ui(i)); Tab(12); Hex(ui(i)); Tab(23); Oct(ui(i)); Tab(31); Bin(ui(i))
|
||||
Next
|
||||
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
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})
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// fmt.Print formats integer types directly as bases 2, 8, 10, and 16.
|
||||
fmt.Printf("%b\n", 13)
|
||||
fmt.Printf("%o\n", 13)
|
||||
fmt.Printf("%d\n", 13)
|
||||
fmt.Printf("%x\n", 13)
|
||||
// big ints work with fmt as well.
|
||||
d := big.NewInt(13)
|
||||
fmt.Printf("%b\n", d)
|
||||
fmt.Printf("%o\n", d)
|
||||
fmt.Printf("%d\n", d)
|
||||
fmt.Printf("%x\n", d)
|
||||
// strconv.FormatInt handles arbitrary bases from 2 to 36 for the
|
||||
// int64 type. There is also strconv.FormatUInt for the uint64 type.
|
||||
// There no equivalent for big ints.
|
||||
fmt.Println(strconv.FormatInt(1313, 19))
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
import Numeric
|
||||
|
||||
main :: IO ()
|
||||
main = mapM_ f [0..33] where
|
||||
f :: Int -> IO ()
|
||||
f n = putStrLn $ " " ++ showOct n "" ++ " " ++ show n ++ " " ++ showHex n ""
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
import Data.List (unfoldr, transpose, intercalate)
|
||||
import Data.Array (Array, listArray, (!))
|
||||
import Data.Monoid ((<>))
|
||||
|
||||
|
||||
-- ARBITRARY RADICES ---------------------------------------
|
||||
bases :: [Int]
|
||||
bases = abs <$> [2, 7, 8, 10, 12, 16, 32]
|
||||
|
||||
tableRows :: [[String]]
|
||||
tableRows = ((([baseDigits] <*> bases) <*>) . return) <$> [1 .. 33]
|
||||
|
||||
digits :: Array Int Char
|
||||
digits = listArray (0, 35) (['0' .. '9'] <> ['A' .. 'Z'])
|
||||
|
||||
baseDigits :: Int -> Int -> String
|
||||
baseDigits base
|
||||
| base > 36 = const "Needs glyphs beyond Z"
|
||||
| otherwise = reverse . unfoldr remQuot
|
||||
where
|
||||
remQuot 0 = Nothing
|
||||
remQuot n =
|
||||
let (q, r) = quotRem n base
|
||||
in Just (digits ! r, q)
|
||||
|
||||
-- TEST AND TABULATION-------------------------------------
|
||||
table :: String -> [[String]] -> [String]
|
||||
table delim rows =
|
||||
intercalate delim <$>
|
||||
transpose
|
||||
((fmap =<< flip justifyRight ' ' . maximum . fmap length) <$> transpose rows)
|
||||
|
||||
justifyRight :: Int -> Char -> String -> String
|
||||
justifyRight n c s = drop (length s) (replicate n c <> s)
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_
|
||||
putStrLn
|
||||
(table " " (([fmap show, fmap $ const "----"] <*> [bases]) <> tableRows))
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
DO n = 1, 33
|
||||
WRITE(Format="b6.0, o4.0, i4.0, z4.0") n, n, n, n
|
||||
ENDDO
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
procedure main()
|
||||
write("Non-decimal radices/Output")
|
||||
every i := 255 | 2 | 5 | 16 do {
|
||||
printf("%%d = %d\n",i) # integer format
|
||||
printf("%%x = %x\n",i) # hex format
|
||||
printf("%%o = %o\n",i) # octal format
|
||||
printf("%%s = %s\n",i) # string format
|
||||
printf("%%i = %i\n",i) # image format
|
||||
}
|
||||
end
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
8 #.inv 4009
|
||||
7 6 5 1
|
||||
-.&' '": 8 #.inv 4009
|
||||
7651
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
require 'convert'
|
||||
hfd 180097588
|
||||
ABC1234
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
var bases = [2, 8, 10, 16, 24];
|
||||
for (var n = 0; n <= 33; n++) {
|
||||
var row = [];
|
||||
for (var i = 0; i < bases.length; i++)
|
||||
row.push( n.toString(bases[i]) );
|
||||
print(row.join(', '));
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
using Primes, Printf
|
||||
|
||||
println("Primes ≤ $hi written in common bases.")
|
||||
@printf("%8s%8s%8s%8s", "bin", "oct", "dec", "hex")
|
||||
for i in primes(50)
|
||||
@printf("%8s%8s%8s%8s\n", bin(i), oct(i), dec(i), hex(i))
|
||||
end
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
include ..\Utilitys.tlhy
|
||||
|
||||
33 [
|
||||
( "decimal: " swap " bin: " over 8 itob reverse ) lprint nl
|
||||
] for
|
||||
|
||||
"End " input
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
// version 1.1.2
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val bases = intArrayOf(2, 8, 10, 16, 19, 36)
|
||||
for (base in bases) print("%6s".format(base))
|
||||
println()
|
||||
println("-".repeat(6 * bases.size))
|
||||
for (i in 0..35) {
|
||||
for (base in bases) print("%6s".format(i.toString(base)))
|
||||
println()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
10 FOR i=1 TO 20
|
||||
20 PRINT i,BIN$(i),HEX$(i)
|
||||
30 NEXT
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
for i = 1, 33 do
|
||||
print( string.format( "%o \t %d \t %x", i, i, i ) )
|
||||
end
|
||||
|
|
@ -0,0 +1 @@
|
|||
fprintf('%3d %3o %3x\n',repmat(1:20,3,1))
|
||||
|
|
@ -0,0 +1 @@
|
|||
Table[IntegerString[n,b], {n,Range@38}, {b,{2,8,16,36}}] // Grid
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
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.
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
import java.util.Formatter
|
||||
|
||||
loop i_ = 1 to 3
|
||||
loop n_ = 20 to 20000 by 2131
|
||||
select case i_
|
||||
when 1 then say useBif(n_)
|
||||
when 2 then say useJavaFormat(n_)
|
||||
when 3 then say useJavaNumber(n_)
|
||||
otherwise nop
|
||||
end
|
||||
end n_
|
||||
say
|
||||
end i_
|
||||
|
||||
return
|
||||
|
||||
-- NetRexx doesn't have a decimal to octal conversion
|
||||
method useBif(n_) public static
|
||||
d_ = '_'
|
||||
return '[Base 16='n_.d2x().right(8)',Base 10='n_.right(8)',Base 8='d_.right(8)',Base 2='n_.d2x().x2b().right(20)']'
|
||||
|
||||
-- Some of Java's java.lang.Number classes have conversion methods
|
||||
method useJavaNumber(n_) public static
|
||||
nx = Long.toHexString(n_)
|
||||
nd = Long.toString(n_)
|
||||
no = Long.toOctalString(n_)
|
||||
nb = Long.toBinaryString(n_)
|
||||
return '[Base 16='Rexx(nx).right(8)',Base 10='Rexx(nd).right(8)',Base 8='Rexx(no).right(8)',Base 2='Rexx(nb).right(20)']'
|
||||
|
||||
-- Java Formatter doesn't have a decimal to binary conversion
|
||||
method useJavaFormat(n_) public static
|
||||
fb = StringBuilder()
|
||||
fm = Formatter(fb)
|
||||
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()
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
import strutils
|
||||
|
||||
for i in 0..33:
|
||||
echo toBin(i, 6)," ",toOct(i, 3)," ",align($i,2)," ",toHex(i,2)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
for n = 0 to 33 do
|
||||
Printf.printf " %3o %2d %2X\n" n n n (* binary not supported *)
|
||||
done
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
printbinary(n)={
|
||||
n=binary(n);
|
||||
for(i=1,#n,print1(n[i]))
|
||||
};
|
||||
printdecimal(n)={
|
||||
print1(n)
|
||||
};
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
foreach (range(0, 33) as $n) {
|
||||
echo decbin($n), "\t", decoct($n), "\t", $n, "\t", dechex($n), "\n";
|
||||
}
|
||||
?>
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
foreach (range(0, 33) as $n) {
|
||||
printf(" %6b %3o %2d %2X\n", $n, $n, $n, $n);
|
||||
}
|
||||
?>
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
get list (n);
|
||||
put skip list (n); /* Prints N in decimal */
|
||||
put skip edit (n) (B); /* prints N as a bit string, N > 0 */
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
foreach my $n (0..33) {
|
||||
printf " %6b %3o %2d %2X\n", $n, $n, $n, $n;
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<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>
|
||||
<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>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
33 for
|
||||
dup "decimal: " print print " bin: " print 8 int>bit print nl
|
||||
endfor
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
(de printNumber (N Base)
|
||||
(when (>= N Base)
|
||||
(printNumber (/ N Base) Base) )
|
||||
(let C (% N Base)
|
||||
(and (> C 9) (inc 'C 39))
|
||||
(prin (char (+ C `(char "0")))) ) )
|
||||
|
||||
(printNumber 26 16))
|
||||
(prinl)
|
||||
(printNumber 123456789012345678901234567890 36))
|
||||
(prinl)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
foreach ($n in 0..33) {
|
||||
"Base 2: " + [Convert]::ToString($n, 2)
|
||||
"Base 8: " + [Convert]::ToString($n, 8)
|
||||
"Base 10: " + $n
|
||||
"Base 10: " + [Convert]::ToString($n, 10)
|
||||
"Base 10: " + ("{0:D}" -f $n)
|
||||
"Base 16: " + [Convert]::ToString($n, 16)
|
||||
"Base 16: " + ("{0:X}" -f $n)
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
>>> 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
|
||||
>>>
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
for n in range(34):
|
||||
print " %3o %2d %2X" % (n, n, n)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
n = 33
|
||||
#Python 3.x:
|
||||
print(bin(n), oct(n), n, hex(n)) # bin() only available in Python 3.x and 2.6
|
||||
# output: 0b100001 0o41 33 0x21
|
||||
|
||||
#Python 2.x:
|
||||
#print oct(n), n, hex(n)
|
||||
# output: 041 33 0x21
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
' [ 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 ]
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
# dec to oct
|
||||
as.octmode(x)
|
||||
# dec to hex
|
||||
as.hexmode(x)
|
||||
# oct or hex to dec
|
||||
as.integer(x)
|
||||
# or
|
||||
as.numeric(x)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
/*REXX pgm shows REXX's ability to show decimal numbers in binary & hex.*/
|
||||
|
||||
do j=0 to 50 /*show some low-value num conversions*/
|
||||
say right(j,3) ' in decimal is',
|
||||
right(d2b(j),12) " in binary",
|
||||
right(d2x(j),12) ' in hexadecimal.'
|
||||
end /*j*/
|
||||
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*/
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
/*REXX program shows REXX's ability to show dec nums in bin/hex/base256.*/
|
||||
|
||||
do j=14 to 67 /*display some lower-value numbers. */
|
||||
say right(j,3) ' in decimal is',
|
||||
right(d2b(j),12) " in binary",
|
||||
right(d2x(j),12) ' in hexadecimal',
|
||||
right(d2c(j),12) ' in base256.'
|
||||
end
|
||||
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*/
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
#lang racket
|
||||
|
||||
;; Explicit conversion of numbers can use the standard radices
|
||||
(map (λ(r) (number->string 123 r)) '(2 8 10 16))
|
||||
;; -> '("1111011" "173" "123" "7b")
|
||||
|
||||
;; There is also the `~r' formatting function that works with any radix
|
||||
;; up to 36
|
||||
(for/list ([r (in-range 2 37)]) (~r 123 #:base r))
|
||||
;; -> '("1111011" "02111" "3231" "344" "323" "432" "173" "641" "123" "201"
|
||||
;; "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")
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
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"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
for 0..33 -> $n {
|
||||
printf " %6b %3o %2d %2X\n", $n xx 4;
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
# Project : Non Decimal radices/Output
|
||||
|
||||
see string(0) + nl
|
||||
see string(123456789) + nl
|
||||
see string(-987654321) + nl
|
||||
|
||||
see upper(hex(43981)) + nl
|
||||
see upper(hex(-1)) + nl
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
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)}"}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
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);
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
object Main extends App {
|
||||
val radices = List(2, 8, 10, 16, 19, 36)
|
||||
for (base <- radices) print(f"$base%6d")
|
||||
println(s"""\n${"-" * (6 * radices.length)}""")
|
||||
for (i <- BigInt(0) to 35; // BigInt has a toString(radix) method
|
||||
radix <- radices;
|
||||
eol = if (radix == radices.last) '\n' else '\0'
|
||||
) print(f"${i.toString(radix)}%6s$eol")
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
(do ((i 0 (+ i 1)))
|
||||
((>= i 33))
|
||||
(display (number->string i 2)) ; binary
|
||||
(display " ")
|
||||
(display (number->string i 8)) ; octal
|
||||
(display " ")
|
||||
(display (number->string i 10)) ; decimal, the "10" is optional
|
||||
(display " ")
|
||||
(display (number->string i 16)) ; hex
|
||||
(newline))
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var integer: i is 0;
|
||||
begin
|
||||
for i range 1 to 33 do
|
||||
writeln(i lpad 6 <&
|
||||
i radix 8 lpad 6 <&
|
||||
i radix 16 lpad 6);
|
||||
end for;
|
||||
end func;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
range(0, 33).each { |n|
|
||||
printf(" %6b %3o %2d %2X\n", ([n]*4)...);
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
1 to: 33 do: [ :i |
|
||||
('%1 %2 %3' % { i printStringRadix: 8. i printStringRadix: 16. i printStringRadix: 2 })
|
||||
printNl.
|
||||
].
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
Local old
|
||||
getMode("Base")→old
|
||||
setMode("Base", "BIN")
|
||||
Disp string(16)
|
||||
setMode("Base", "HEX")
|
||||
Disp string(16)
|
||||
setMode("Base", "DEC")
|
||||
Disp string(16)
|
||||
setMode("Base", old)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
0b10000
|
||||
0h10
|
||||
16
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
for {set n 0} {$n <= 33} {incr n} {
|
||||
puts [format " %3o %2d %2X" $n $n $n]
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
# 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
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
import "/fmt" for Conv, Fmt
|
||||
|
||||
System.print(" 2 7 8 10 12 16 32")
|
||||
System.print("------ ---- ---- ---- ---- ---- ----")
|
||||
for (i in 1..33) {
|
||||
var b2 = Fmt.b(6, i)
|
||||
var b7 = Fmt.s(4, Conv.itoa(i, 7))
|
||||
var b8 = Fmt.o(4, i)
|
||||
var b10 = Fmt.d(4, i)
|
||||
var b12 = Fmt.s(4, Conv.Itoa(i, 12))
|
||||
var b16 = Fmt.X(4, i)
|
||||
var b32 = Fmt.s(4, Conv.Itoa(i, 32))
|
||||
System.print("%(b2) %(b7) %(b8) %(b10) %(b12) %(b16) %(b32)")
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
include c:\cxpl\codes;
|
||||
int N;
|
||||
[N:= 2;
|
||||
repeat HexOut(0, N); Text(0, " ");
|
||||
IntOut(0, N); CrLf(0);
|
||||
N:= N*N;
|
||||
until N=0;
|
||||
]
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
for i = 1 to 33
|
||||
print "decimal: ", i, " hex: ", hex$(i), " bin: ", bin$(i)
|
||||
next
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
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()}
|
||||
|
|
@ -0,0 +1 @@
|
|||
(100).toString(36) //-->"2s"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
"%,.2B".fmt(1234567) //-->"1|0010|1101|0110|1000|0111"
|
||||
"%,d".fmt(1234567) //-->"1,234,567"
|
||||
"%,x".fmt(1234567) //-->"12|d6|87"
|
||||
Loading…
Add table
Add a link
Reference in a new issue