Non-decimal radices/Convert: Difference between revisions

no edit summary
m (syntax highlighting fixup automation)
No edit summary
 
(14 intermediate revisions by 8 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 2,043 ⟶ 2,168:
def convert(base):
def stream:
recurse(if . >= 0base then ./base|floor else empty end) | . % base ;
[stream] | reverse
if . == 0 then "0"
else [stream] | reverse | .[1:]
| if base < 10 then map(tostring) | join("")
elif base <= 36 then map(if . < 10 then 48 + . else . + 87 end) | implode
else error("base too large")
end;
end;
 
# input string is converted from "base" to an integer, within limits
Line 2,252 ⟶ 2,375:
Only had to write 'dec2base' as the reverse is provided by the in-built function 'tonumber'
<syntaxhighlight lang="lua">function dec2base (base, n)
local result, digit = ""
while n > 0 dorepeat
local digit = n % base
if digit > 9 then digit = string.char(digit + 87) end
n digit = mathstring.floorchar(ndigit /+ base87)
end
result = digit .. result
n = n // base
end
until n == 0
return result
end
Line 2,524 ⟶ 2,649:
- : int = 26
 
A real base conversion exampleimplementation: {{trans|Haskell}}
<syntaxhighlight lang="ocaml">let basen_of_int b n : string =
let tab = "0123456789abcdefghijklmnopqrstuvwxyz" in
let rec aux x l =
if x < b
then tab.[x] :: l
else aux (x / b) (tab.[x mod b] :: l)
in
String.of_seq (List.to_seq (aux n []))
 
let basen_to_int b ds : int =
<syntaxhighlight lang="ocaml">let to_base b v =
let recof_sym to_base' a vc =
ifint_of_char vc =- 0match thenc with
| '0' .. '9' -> int_of_char '0'
a
| 'a' .. 'z' -> int_of_char 'a' - 10
else
| to_base'A' (v.. mod'Z' b-> ::int_of_char a) (v'A' /- b)10
| _ -> invalid_arg "unkown digit"
in
String.fold_left (fun n d -> n * b + of_sym d) 0 ds</syntaxhighlight>
to_base' [] v
 
let from_base b ds =
List.fold_left (fun n k -> n * b + k) 0 ds
 
let to_alpha_digit n =
if n < 10 then
char_of_int (n + int_of_char '0')
else
char_of_int (n + int_of_char 'a' - 10)
 
let to_alpha_digits ds =
let buf = Buffer.create (List.length ds) in
List.iter (fun i -> Buffer.add_char buf (to_alpha_digit i)) ds;
Buffer.contents buf
 
let from_alpha_digit c = match c with
'0'..'9' -> int_of_char c - int_of_char '0'
| 'A'..'Z' -> int_of_char c - int_of_char 'A' + 10
| 'a'..'z' -> int_of_char c - int_of_char 'a' + 10
 
let from_alpha_digits s =
let result = ref [] in
String.iter (fun c -> result := from_alpha_digit c :: !result) s;
List.rev !result</syntaxhighlight>
 
Example:
 
<pre>
# to_alpha_digits (to_basebasen_of_int 16 42)26;;
- : string = "2a1a"
# from_basebasen_to_int 16 (from_alpha_digits "2a1a");;
- : int = 4226
</pre>
 
Line 2,652 ⟶ 2,761:
my($n,$b) = @_;
my $s = "";
while ($n)do {
$s .= ('0'..'9','a'..'z')[$n % $b]; . $s
} while $n = int($n / $b);
}$s
scalar(reverse($s));
}
sub base_from {
Line 3,054 ⟶ 3,162:
=={{header|Quackery}}==
 
Handles radices in the range 2 to 36.
The built-in word <code>number$</code> (included in listing) provides conversion of a number to a string in the current <code>base</code>. The valid range of bases is 2 to 36 inclusive, digits greater than 9 are represented by upper-case letters. The word <code>>base$</code> adapts <code>number$</code>to the requirements of the task by temporarily overriding the current <code>base</code> and converting the returned string from upper to lower case.
 
<syntaxhighlight lang="quackeryQuackery">( [ $[ ''base overput abs
[ base share /mod digit
rot join swap
dup 0 = until ]
drop
swap 0 < if
[ $ '-' swap join ] ] is number$ ( n --> $ )
 
[ base put
number$
base release
$ ''"" swap
witheach
[ lower join ] ] is >base$ base_to_string ( n bn --> $ )
 
[ base put
say "The number 2970609818455516403037 in hexatrigesimal is "
$->n drop
2970609818455516403037 36 >base$ echo$
say "." base release ] is string_to_base ( $ n --> n )</syntaxhighlight>
 
{{out}}
 
As a dialogue in the quackery shell.
<pre>The number 2970609818455516403037 in hexatrigesimal is hexatrigesimal.</pre>
 
<pre>/O> $ "sesquipedalian" 36 string_to_base
...
 
Stack: 4846409295160778886623
 
/O> 36 base_to_string echo$ cr
...
sesquipedalian
 
Stack empty.
</pre>
 
=={{header|R}}==
Line 3,149 ⟶ 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,268 ⟶ 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,373 ⟶ 3,508:
 
def bigToBase(num: BigInt, newBase: Int): String = num.toString(newBase)</syntaxhighlight>
 
=={{header|Scheme}}==
R7RS specifies only a radix of 2, 8, 10, or 16 for the functions below. However, some implementations support arbitrary (e.g. Chibi-Scheme or Guile).
<syntaxhighlight lang="scheme">
(number->string 26 16)
 
(string->number "1a" 16)</syntaxhighlight>
 
=={{header|Seed7}}==
Line 3,627 ⟶ 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