Non-decimal radices/Convert: Difference between revisions

no edit summary
m (→‎{{header|jq}}: simplify)
No edit summary
 
(8 intermediate revisions by 6 users not shown)
Line 1,260:
{{out}}
109517
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function GetRadixString(L: Integer; Radix: Byte): string;
{Converts integer a string of any radix}
const RadixChars: array[0..35] Of char =
('0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
'G','H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z');
var I: integer;
var S: string;
var Sign: string[1];
begin
Result:='';
If (L < 0) then
begin
Sign:='-';
L:=Abs(L);
end
else Sign:='';
S:='';
repeat
begin
I:=L mod Radix;
S:=RadixChars[I] + S;
L:=L div Radix;
end
until L = 0;
Result:=Sign + S;
end;
 
procedure ShowRadixConvertion(Memo: TMemo);
var B,N: integer;
var S,RS: string;
begin
N:=6502;
for B:=2 to 23 do
begin
RS:=GetRadixString(N,B);
RS:=LowerCase(RS);
Memo.Lines.Add(Format('%5d -> base: %3D = %15S',[N,B,RS]));
end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
6502 -> base: 2 = 1100101100110
6502 -> base: 3 = 22220211
6502 -> base: 4 = 1211212
6502 -> base: 5 = 202002
6502 -> base: 6 = 50034
6502 -> base: 7 = 24646
6502 -> base: 8 = 14546
6502 -> base: 9 = 8824
6502 -> base: 10 = 6502
6502 -> base: 11 = 4981
6502 -> base: 12 = 391a
6502 -> base: 13 = 2c62
6502 -> base: 14 = 2526
6502 -> base: 15 = 1dd7
6502 -> base: 16 = 1966
6502 -> base: 17 = 1588
6502 -> base: 18 = 1214
6502 -> base: 19 = i04
6502 -> base: 20 = g52
6502 -> base: 21 = efd
6502 -> base: 22 = d9c
6502 -> base: 23 = c6g
</pre>
 
 
=={{header|E}}==
Line 1,272 ⟶ 1,350:
? integerToString(200, 16)
# value: "c8"</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func$ num2str n base .
if n = 0
return "0"
.
d = n mod base
if d > 9
d += 39
.
d$ = strchar (d + 48)
if n < base
return d$
.
return num2str (n div base) base & d$
.
func str2num s$ base .
r = 0
for c$ in strchars s$
d = strcode c$ - 48
if d > 9
d -= 39
.
r = r * base + d
.
return r
.
print num2str 253 16
print str2num "fd" 16
print num2str 0 16
</syntaxhighlight>
 
=={{header|Elixir}}==
Line 2,038 ⟶ 2,148:
"devanagariDecimal": "२४०"
}</pre>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">DEFINE
digit == "0123456789abcdefghijklmnopqrstuvwxyz" of;
itostr ==
"" rollup
[>=] [dup rollup div digit rotated swons rollup] while
pop digit swons.
 
26 16 itostr.
"1a" 16 strtol.
</syntaxhighlight>
{{out}}
<pre>"1a"
26</pre>
 
=={{header|jq}}==
Line 3,134 ⟶ 3,259:
$num.base($base);
}</syntaxhighlight>
These work on any real type including integer types. There is also a build in method/function for Strings: [https://docs.raku.org/routine/parse-base parse-base].
 
=={{header|REXX}}==
Line 3,253 ⟶ 3,378:
17f (base 16) -> 383 (decimal)
101111111 (base 2) -> 383 (decimal)
</pre>
 
=={{header|RPL}}==
≪ → base
≪ "" SWAP
'''WHILE''' DUP '''REPEAT'''
base MOD LAST / FLOOR
SWAP DUP 9 > 87 48 IFTE + CHR
ROT + SWAP
'''END''' DROP
≫ ≫ ‘'''D→B'''’ STO
≪ → number base
≪ 0 1 number SIZE '''FOR''' j
base * number j DUP SUB
NUM DUP 57 > 87 48 IFTE - +
'''NEXT'''
≫ ≫ ‘'''B→D'''’ STO
 
"r0setta" 36 '''B→D'''
DUP 36 '''D→B'''
{{out}}
<pre>
2: 58820844142
1: "r0setta"
</pre>
 
Line 3,619 ⟶ 3,769:
{{libheader|Wren-fmt}}
The methods Conv.itoa and Conv.atoi in the above module provide the required functionality.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Conv
 
System.print(Conv.itoa(26, 16))
3

edits