Non-decimal radices/Convert: Difference between revisions

no edit summary
(better ocaml example)
No edit summary
Line 9:
=={{header|Ada}}==
Ada provides built-in capability to convert between all bases from 2 through 16. This task requires conversion for bases up to 36. The following program demonstrates such a conversion using an iterative solution.
<lang ada>with Ada.Text_Io; use Ada.Text_Io;
with Ada.Strings.Fixed;
With Ada.Strings.Unbounded;
Line 62:
Put_Line("26 converted to base 16 is " & To_Base(26, 16));
Put_line("1a (base 16) is decimal" & Integer'image(To_Decimal("1a", 16)));
end Number_Base_Conversion;</adalang>
 
=={{header|C++}}==
<lang cpp>#include <limits>
#include <string>
#include <cassert>
Line 98:
result = result * base + digits.find(num_str[pos]);
return result;
}</cpplang>
 
=={{header|Common Lisp}}==
<lang lisp>(let ((*print-base* 16)
*read-base* 16))
(write-to-string 26) ; returns the string "1A"
(read-from-string "1a") ; returns the integer 26
 
(write-to-string 26 :base 16) ; also "1A"</lisplang>
 
=={{header|D}}==
D standard library string module included functions to convert number to string at a radix.
<lang d>module std.string;
char[] toString(long value, uint radix);
char[] toString(ulong value, uint radix);</dlang>
Implementation.
<lang d>module radixstring ;
import std.stdio ;
import std.ctype ;
Line 168:
else writefln() ;
writefln(ItoA(60272032366,36)," ",ItoA(591458,36)) ;
}</dlang>
=={{header|Forth}}==
Forth has a global user variable, BASE, which determines the radix used for parsing, interpretation, and printing of integers. This can handle bases from 2-36, but there are two words to switch to the most popular bases, DECIMAL and HEX.
Line 296:
=={{header|Java}}==
for long's:
<lang java>public static long backToTen(String num, int oldBase){
return Long.parseLong(num, oldBase); //takes both uppercase and lowercase letters
}
Line 302:
public static String tenToBase(long num, int newBase){
return Long.toString(num, newBase);//add .toUpperCase() for capital letters
}</javalang>
 
for BigInteger's:
<lang java>public static BigInteger backToTenBig(String num, int oldBase){
return new BigInteger(num, oldBase); //takes both uppercase and lowercase letters
}
Line 311:
public static String tenBigToBase(BigInteger num, int newBase){
return num.toString(newBase);//add .toUpperCase() for capital letters
}</javalang>
 
=={{header|JavaScript}}==
<lang javascript>k = 26
s = k.toString(16) //gives 1a
i = parseInt('1a',16) //gives 26
//optional special case for hex:
i = +('0x'+s) //hexadecimal base 16, if s='1a' then i=26.</javascriptlang>
 
=={{header|OCaml}}==
<lang ocaml>let int_of_basen n str =
match n with
| 16 -> int_of_string("0x" ^ str)
Line 332:
| 16 -> Printf.sprintf "%x" d
| 8 -> Printf.sprintf "%o" d
| _ -> failwith "unhandled"</ocamllang>
 
# basen_of_int 16 26 ;;
Line 342:
A real base conversion example: {{trans|Haskell}}
 
<lang ocaml>let to_base b v =
let rec to_base' a v =
if v = 0 then
Line 373:
let result = ref [] in
String.iter (fun c -> result := from_alpha_digit c :: !result) s;
List.rev !result</ocamllang>
 
Example:
Line 386:
=={{header|Perl}}==
Perl has some built-in capabilites for various conversions between decimal, hexadecimal, octal, and binary, but not other bases.
<lang perl>sub digitize
# Converts an integer to a single digit.
{my $i = shift;
Line 413:
for (my $n = 0 ; $numeral ; ++$n)
{$int += $radix**$n * undigitize substr($numeral, 0, 1, '');}
return $int;}</perllang>
 
=={{header|PHP}}==
PHP has a base_convert() function that directly converts between strings of one base and strings of another base:
<lang php>base_convert("26", 10, 16); // returns "1a"</phplang>
 
If you want to convert a string to an integer, the intval() function optionally takes a base argument when given a string:
<lang php>intval("1a", 16); // returns 26</phplang>
 
To go the other way around, I guess you can use base_convert() again; I am unaware of a better way:
<lang php>base_convert(26, 10, 16); // returns "1a"</phplang>
 
In addition, there are specialized functions for converting certain bases:
<lang php>// converts int to binary string
decbin(26); // returns "11010"
// converts int to octal string
Line 437:
octdec("32"); // returns 26
// converts hex string to int
hexdec("1a"); // returns 26</phplang>
 
=={{header|Pop11}}==
Line 465:
 
=={{header|Python}}==
<lang python>def baseN(num,b):
return ((num == 0) and "0" ) or ( baseN(num // b, b).lstrip("0") + "0123456789abcdefghijklmnopqrstuvwxyz"[num % b])
k = 26
s = baseN(k,16) # returns the string 1a
i = int('1a',16) # returns the integer 26</pythonlang>
 
=={{header|Ruby}}==
<lang ruby>s = 26.to_s(16) # returns the string 1a
i = '1a'.to_i(16) # returns the integer 26</rubylang>