tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,5 @@
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 [[Common number base parsing]].

View file

@ -0,0 +1,2 @@
---
note: Arithmetic operations

View file

@ -0,0 +1,5 @@
main:(
FOR i TO 33 DO
printf(($10r6d," "16r6d," "8r6dl$, BIN i, BIN i, BIN i))
OD
)

View file

@ -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

View file

@ -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;

View file

@ -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
}

View file

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

View file

@ -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;
}

View file

@ -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;
}

View file

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

View file

@ -0,0 +1,2 @@
(loop for n from 0 to 33 do
(format t " ~6B ~3O ~2D ~2X~%" n n n n))

View file

@ -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);
}

View file

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

View file

@ -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()
}

View file

@ -0,0 +1,3 @@
for i = 1 to 33 do
printf(1,"%6d %6x %6o\n",{i,i,i})
end for

View file

@ -0,0 +1 @@
1234567 2 36 [a,b] [ >base print ] with each

View file

@ -0,0 +1,5 @@
: main 34 1 do cr i dec. i hex. loop ;
main
...
11 $B
...

View file

@ -0,0 +1,3 @@
: base. ( n base -- ) base @ >r base ! . r> base ! ;
: oct. ( n -- ) 8 base. ;
: bin. ( n -- ) 2 base. ;

View file

@ -0,0 +1,3 @@
do n = 1, 33
write(*, "(b6, o4, i4, z4)") n, n, n, n
end do

View file

@ -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})

View file

@ -0,0 +1,28 @@
package main
import (
"fmt"
"math/big"
"strconv"
)
func main() {
// package strconv:
// Itoa is the most common int to string conversion. it is base 10 only.
x := strconv.Itoa(13)
fmt.Printf("%q\n", x)
// FormatInt handles arbitrary bases from 2 to 36.
x = strconv.FormatInt(1313, 19)
fmt.Printf("%q\n", x)
// package fmt: allows direct conversion from integer
// to string types for 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)
// package big: string conversion is base 10 only.
fmt.Println(big.NewInt(13))
}

View file

@ -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

View file

@ -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 ""

View file

@ -0,0 +1,3 @@
DO n = 1, 33
WRITE(Format="b6.0, o4.0, i4.0, z4.0") n, n, n, n
ENDDO

View file

@ -0,0 +1,10 @@
rocedure 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

View file

@ -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

View file

@ -0,0 +1,4 @@
8 #.inv 4009
7 6 5 1
-.&' '": 8 #.inv 4009
7651

View file

@ -0,0 +1,3 @@
require 'convert'
hfd 180097588
ABC1234

View file

@ -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
}
}

View file

@ -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(', '));
}

View file

@ -0,0 +1,3 @@
10 FOR i=1 TO 20
20 PRINT i,BIN$(i),HEX$(i)
30 NEXT

View file

@ -0,0 +1,3 @@
for i = 1, 33 do
print( string.format( "%o \t %d \t %x", i, i, i ) )
end

View file

@ -0,0 +1,2 @@
Scan[Print[IntegerString[#, 2], ",", IntegerString[#, 8],
",",#, ",",IntegerString[#, 16],",", IntegerString[#, 36]]&, Range[38]]

View file

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

View file

@ -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()

View file

@ -0,0 +1,3 @@
for n = 0 to 33 do
Printf.printf " %3o %2d %2X\n" n n n (* binary not supported *)
done

View file

@ -0,0 +1,7 @@
printbinary(n)={
n=binary(n);
for(i=1,#n,print1(n[i]))
};
printdecimal(n)={
print1(n)
};

View file

@ -0,0 +1,5 @@
<?php
foreach (range(0, 33) as $n) {
echo decbin($n), "\t", decoct($n), "\t", $n, "\t", dechex($n), "\n";
}
?>

View file

@ -0,0 +1,5 @@
<?php
foreach (range(0, 33) as $n) {
printf(" %6b %3o %2d %2X\n", $n, $n, $n, $n);
}
?>

View file

@ -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 */

View file

@ -0,0 +1,3 @@
for 0..33 -> $n {
printf " %6b %3o %2d %2X\n", $n xx 4;
}

View file

@ -0,0 +1,3 @@
foreach my $n (0..33) {
printf " %6b %3o %2d %2X\n", $n, $n, $n, $n;
}

View file

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

View file

@ -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)
}

View file

@ -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

View file

@ -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
>>>

View file

@ -0,0 +1,2 @@
for n in range(34):
print " %3o %2d %2X" % (n, n, n)

View file

@ -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

View file

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

View file

@ -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*/

View file

@ -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*/

View file

@ -0,0 +1,38 @@
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

View file

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

View file

@ -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;

View file

@ -0,0 +1,4 @@
1 to: 33 do: [ :i |
('%1 %2 %3' % { i printStringRadix: 8. i printStringRadix: 16. i printStringRadix: 2 })
printNl.
].

View file

@ -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

View file

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

View file

@ -0,0 +1,3 @@
0b10000
0h10
16

View file

@ -0,0 +1,3 @@
for {set n 0} {$n <= 33} {incr n} {
puts [format " %3o %2d %2X" $n $n $n]
}

View file

@ -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
}

View file

@ -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;
]