Non-decimal radices/Convert: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added/tested 0(16) example) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 285: | Line 285: | ||
end |
end |
||
end.</lang> |
end.</lang> |
||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
Line 579: | Line 578: | ||
<pre>-9223372036854775808 in base 2: -1000000000000000000000000000000000000000000000000000000000000000 |
<pre>-9223372036854775808 in base 2: -1000000000000000000000000000000000000000000000000000000000000000 |
||
383 in base 16: 17f</pre> |
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#}}== |
=={{header|C sharp|C#}}== |
||
Line 742: | Line 710: | ||
} |
} |
||
</lang> |
</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}}== |
=={{header|Caché ObjectScript}}== |
||
Line 2,338: | Line 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. |
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}}== |
=={{header|Phix}}== |
||
Line 2,427: | Line 2,416: | ||
// converts hex string to int |
// converts hex string to int |
||
hexdec("1a"); // returns 26</lang> |
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}}== |
=={{header|PicoLisp}}== |
||
Line 2,471: | Line 2,444: | ||
26 |
26 |
||
"byw97um9s91dlz68tsi"</pre> |
"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}}== |
=={{header|Pop11}}== |
||
Line 2,492: | Line 2,481: | ||
incharitem(stringin(base >< ':' >< s))(); |
incharitem(stringin(base >< ':' >< s))(); |
||
enddefine;</lang> |
enddefine;</lang> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
<lang PureBasic>Global alphanum$ = "0123456789abcdefghijklmnopqrstuvwxyz" ;36 digits |
<lang PureBasic>Global alphanum$ = "0123456789abcdefghijklmnopqrstuvwxyz" ;36 digits |
||
Line 2,624: | Line 2,614: | ||
;; (random-test) |
;; (random-test) |
||
</lang> |
</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}}== |
=={{header|REXX}}== |
||
Line 3,008: | Line 3,009: | ||
Debug.Print "26 decimal in base 16 is: "; to_base(26, 16); ". Conversely, hexadecimal 1a in decimal is: "; from_base("1a", 16) |
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> |
End Sub</lang>{{out}}<pre>26 decimal in base 16 is: 1a. Conversely, hexadecimal 1a in decimal is: 26 </pre> |
||
=={{header|Wolframalpha}}== |
=={{header|Wolframalpha}}== |
||
input box: 1801 decimal to base 16<br> |
input box: 1801 decimal to base 16<br> |