Non-decimal radices/Convert: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
m (→‎{{header|Phix}}: added/tested 0(16) example)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 285:
end
end.</lang>
 
 
=={{header|AppleScript}}==
Line 579 ⟶ 578:
<pre>-9223372036854775808 in base 2: -1000000000000000000000000000000000000000000000000000000000000000
383 in base 16: 17f</pre>
 
=={{header|C++}}==
<lang cpp>#include <string>
#include <cstdlib>
#include <algorithm>
#include <cassert>
 
std::string const digits = "0123456789abcdefghijklmnopqrstuvwxyz";
 
std::string to_base(unsigned long num, int base)
{
if (num == 0)
return "0";
std::string result;
while (num > 0) {
std::ldiv_t temp = std::div(num, (long)base);
result += digits[temp.rem];
num = temp.quot;
}
std::reverse(result.begin(), result.end());
return result;
}
 
unsigned long from_base(std::string const& num_str, int base)
{
unsigned long result = 0;
for (std::string::size_type pos = 0; pos < num_str.length(); ++pos)
result = result * base + digits.find(num_str[pos]);
return result;
}</lang>
 
=={{header|C sharp|C#}}==
Line 742 ⟶ 710:
}
</lang>
 
=={{header|C++}}==
<lang cpp>#include <string>
#include <cstdlib>
#include <algorithm>
#include <cassert>
 
std::string const digits = "0123456789abcdefghijklmnopqrstuvwxyz";
 
std::string to_base(unsigned long num, int base)
{
if (num == 0)
return "0";
std::string result;
while (num > 0) {
std::ldiv_t temp = std::div(num, (long)base);
result += digits[temp.rem];
num = temp.quot;
}
std::reverse(result.begin(), result.end());
return result;
}
 
unsigned long from_base(std::string const& num_str, int base)
{
unsigned long result = 0;
for (std::string::size_type pos = 0; pos < num_str.length(); ++pos)
result = result * base + digits.find(num_str[pos]);
return result;
}</lang>
 
=={{header|Caché ObjectScript}}==
Line 2,338 ⟶ 2,337:
 
The module [https://metacpan.org/pod/Math::Fleximal Math::Fleximal] not only does very arbitrary base conversion, but allows computations in different bases.
 
=={{header|Perl 6}}==
<lang perl6>sub from-base(Str $str, Int $base) {
+":$base\<$str>";
}
 
sub to-base(Real $num, Int $base) {
$num.base($base);
}</lang>
These work on any real type including integer types.
 
=={{header|Phix}}==
Line 2,427 ⟶ 2,416:
// converts hex string to int
hexdec("1a"); // returns 26</lang>
 
=={{header|PL/I}}==
<lang PL/I>
convert: procedure (N, base) returns (character (64) varying) recursive;
declare N fixed binary (31), base fixed binary;
declare table (0:15) character (
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
declare s character (64) varying;
 
if N = 0 then return ('');
 
s = convert(N/base, base);
return (s || table(mod(N, base)) );
end convert;
</lang>
 
=={{header|PicoLisp}}==
Line 2,471 ⟶ 2,444:
26
"byw97um9s91dlz68tsi"</pre>
 
=={{header|PL/I}}==
<lang PL/I>
convert: procedure (N, base) returns (character (64) varying) recursive;
declare N fixed binary (31), base fixed binary;
declare table (0:15) character (
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
declare s character (64) varying;
 
if N = 0 then return ('');
 
s = convert(N/base, base);
return (s || table(mod(N, base)) );
end convert;
</lang>
 
=={{header|Pop11}}==
Line 2,492 ⟶ 2,481:
incharitem(stringin(base >< ':' >< s))();
enddefine;</lang>
 
=={{header|PureBasic}}==
<lang PureBasic>Global alphanum$ = "0123456789abcdefghijklmnopqrstuvwxyz" ;36 digits
Line 2,624 ⟶ 2,614:
;; (random-test)
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>sub from-base(Str $str, Int $base) {
+":$base\<$str>";
}
 
sub to-base(Real $num, Int $base) {
$num.base($base);
}</lang>
These work on any real type including integer types.
 
=={{header|REXX}}==
Line 3,008 ⟶ 3,009:
Debug.Print "26 decimal in base 16 is: "; to_base(26, 16); ". Conversely, hexadecimal 1a in decimal is: "; from_base("1a", 16)
End Sub</lang>{{out}}<pre>26 decimal in base 16 is: 1a. Conversely, hexadecimal 1a in decimal is: 26 </pre>
 
=={{header|Wolframalpha}}==
input box: 1801 decimal to base 16<br>
10,327

edits