Non-decimal radices/Convert: Difference between revisions

Added solution for Action!
(Add BCPL)
(Added solution for Action!)
Line 61:
(defun show-num (num base)
(coerce (reverse (num-to-cs num base)) 'string))</lang>
 
=={{header|Action!}}==
<lang Action!>CHAR ARRAY digits="0123456789abcdefghijklmnopqrstuvwxyz"
 
PROC CheckBase(BYTE b)
IF b<2 OR b>digits(0) THEN
PrintE("Base is out of range!")
Break()
FI
RETURN
 
PROC Encode(CARD v BYTE b CHAR ARRAY s)
CARD d
BYTE i,len
CHAR tmp
 
CheckBase(b)
len=0
DO
d=v MOD b
len==+1
s(len)=digits(d+1)
v==/b
UNTIL v=0
OD
s(0)=len
 
FOR i=1 to len/2
DO
tmp=s(i)
s(i)=s(len-i+1)
s(len-i+1)=tmp
OD
RETURN
 
CARD FUNC Decode(CHAR ARRAY s BYTE b)
CARD res
BYTE i,j,found
 
CheckBase(b)
res=0
FOR i=1 TO s(0)
DO
found=0
FOR j=1 TO digits(0)
DO
IF digits(j)=s(i) THEN
found=1 EXIT
FI
OD
IF found=0 THEN
PrintE("Unrecognized character!")
Break()
FI
res==*b
res==+j-1
OD
RETURN (res)
 
PROC Main()
CARD v=[6502],v2
BYTE b
CHAR ARRAY s(256)
 
FOR b=2 TO 23
DO
Encode(v,b,s)
v2=Decode(s,b)
PrintF("%U -> base %B %S -> %U%E",v,b,s,v2)
OD
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Non-decimal_radices_convert.png Screenshot from Atari 8-bit computer]
<pre>
6502 -> base 2 1100101100110 -> 6502
6502 -> base 3 22220211 -> 6502
6502 -> base 4 1211212 -> 6502
6502 -> base 5 202002 -> 6502
6502 -> base 6 50034 -> 6502
6502 -> base 7 24646 -> 6502
6502 -> base 8 14546 -> 6502
6502 -> base 9 8824 -> 6502
6502 -> base 10 6502 -> 6502
6502 -> base 11 4981 -> 6502
6502 -> base 12 391a -> 6502
6502 -> base 13 2c62 -> 6502
6502 -> base 14 2526 -> 6502
6502 -> base 15 1dd7 -> 6502
6502 -> base 16 1966 -> 6502
6502 -> base 17 1588 -> 6502
6502 -> base 18 1214 -> 6502
6502 -> base 19 i04 -> 6502
6502 -> base 20 g52 -> 6502
6502 -> base 21 efd -> 6502
6502 -> base 22 d9c -> 6502
6502 -> base 23 c6g -> 6502
</pre>
 
=={{header|Ada}}==
Anonymous user