Non-decimal radices/Convert

From Rosetta Code

Jump to: navigation, search
Non-decimal radices/Convert is a programming task. Visitors like you are encouraged to solve it according to the task description, using any language they may happen to know.
Add to BlogMarksAdd to del.icio.usAdd to diggAdd to NewsvineAdd to redditAdd to Slashdot
Number base conversion is when you express a stored integer in an integer base, such as in octal (base 8) or binary (base 2). It also is involved when you take a string representing a number in a given base and convert it to the stored integer form. Normally, a stored integer is in binary, but that's typically invisible to the user, who normally enters or sees stored integers as decimal.

Write a function (or identify the built-in function) which is passed a non-negative integer to convert, and another integer representing the base. It should return a string containing the digits of the resulting number, without leading zeros except for the number 0 itself. For the digits beyond 9, one should use the lowercase English alphabet, where the digit a = 9+1, b = a+1, etc. The decimal number 26 expressed in base 16 would be 1a, for example.

Write a second function which is passed a string and an integer base, and it returns an integer representing that string interpreted in that base.

The programs may be limited by the word size or other such constraint of a given language. There is no need to do error checking for negatives, bases less than 2, or inappropriate digits.

Contents

[edit] 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.

with Ada.Text_Io; use Ada.Text_Io;
with Ada.Strings.Fixed;
With Ada.Strings.Unbounded;
 
procedure Number_Base_Conversion is
Max_Base : constant := 36;
subtype Base_Type is Integer range 2..Max_Base;
Num_Digits : constant String := "0123456789abcdefghijklmnopqrstuvwxyz";
Invalid_Digit : exception;
 
function To_Decimal(Value : String; Base : Base_Type) return Integer is
use Ada.Strings.Fixed;
Result : Integer := 0;
Decimal_Value : Integer;
Radix_Offset : Natural := 0;
begin
for I in reverse Value'range loop
Decimal_Value := Index(Num_Digits, Value(I..I)) - 1;
if Decimal_Value < 0 then
raise Invalid_Digit;
end if;
Result := Result + (Base**Radix_Offset * Decimal_Value);
Radix_Offset := Radix_Offset + 1;
end loop;
return Result;
end To_Decimal;
 
function To_Base(Value : Natural; Base : Base_Type) return String is
use Ada.Strings.Unbounded;
Result : Unbounded_String := Null_Unbounded_String;
Temp : Natural := Value;
Base_Digit : String(1..1);
begin
if Temp = 0 then
return "0";
end if;
while Temp > 0 loop
Base_Digit(1) := Num_Digits((Temp mod Base) + 1);
if Result = Null_Unbounded_String then
Append(Result, Base_Digit);
else
Insert(Source => Result,
Before => 1,
New_Item => Base_Digit);
end if;
Temp := Temp / Base;
end loop;
return To_String(Result);
end To_Base;
 
begin
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;

[edit] ALGOL 68

[edit] Built in or standard distribution routines

Works with: ALGOL 68 version Standard - no extensions to language used Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386 The formatted transput in ALGOL 68 uses the format type (mode). This format type has many similarities with modern regular expressions and can be used to convert string patterns to and from many of the built in types (modes) in ALGOL 68. Here is an example converting a numbers base.

INT base = 16, from dec = 26;
BITS to bits;
 
FORMAT hex repr = $n(base)r2d$;
 
FILE f; STRING str;
 
associate(f, str);
putf(f, (hex repr, BIN from dec));
print(("Hex: ",str, new line));
 
reset(f);
getf(f, (hex repr, to bits));
print(("Int: ",ABS to bits, new line))

Output:

Hex: 1a
Int:         +26

Note that the only conversions "officially" available are for the bases 2r, 4r, 8r and 16r. But ALGOL 68G allows formatting for all numbers in the range 2r to 16r.

[edit] Implementation example

Handles signed and unsigned numbers from all bases.

Translation of: python

Works with: ALGOL 68 version Standard - no extensions to language used Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386 Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386

STRING numeric alpha = "0123456789abcdefghijklmnopqrstuvwxyz";
 
PROC raise value error = ([]STRING args)VOID: (
put(stand error, "Value error");
STRING sep := ": ";
FOR index TO UPB args - 1 DO put(stand error, (sep, args[index])); sep:=", " OD;
new line(stand error);
stop
);
 
PROC base n = (INT num, base)STRING: (
PROC base n = (INT num, base)STRING:
( num = 0 | "" | base n(num OVER base, base) + numeric alpha[@0][num MOD base]);
( num = 0 | "0" |: num > 0 | base n(num, base) | "-" + base n(-num, base) )
);
 
PROC unsigned int = (STRING repr, INT base)INT:
IF UPB repr < LWB repr THEN 0 ELSE
INT pos;
IF NOT char in string(repr[UPB repr], pos, numeric alpha) THEN
raise value error("CHAR """+repr[UPB repr]+""" not valid")
FI;
unsigned int(repr[:UPB repr-1], base) * base + pos - 1
FI
;
 
PROC int = (STRING repr, INT base)INT:
( repr[LWB repr]="-" | -unsigned int(repr[LWB repr + 1:], base) | unsigned int(repr, base) );
 
[]INT test = (-256, -255, -26, -25, 0, 25, 26, 255, 256);
FOR index TO UPB test DO
INT k = test[index];
STRING s = base n(k,16); # returns the string 1a #
INT i = int(s,16); # returns the integer 26 #
print((k," => ", s, " => ", i, new line))
OD

Output:

       -256 => -100 =>        -256
       -255 => -ff =>        -255
        -26 => -1a =>         -26
        -25 => -19 =>         -25
         +0 => 0 =>          +0
        +25 => 19 =>         +25
        +26 => 1a =>         +26
       +255 => ff =>        +255
       +256 => 100 =>        +256

[edit] Other libraries or implementation specific extensions

As of February 2009 no open source libraries to do this task have been located.

[edit] AutoHotkey

MsgBox % number2base(200, 16) ; 12
MsgBox % parse(200, 16) ; 512
 
number2base(number, base)
{
While, base < digit := floor(number / base)
{
result := mod(number, base) . result
number := digit
}
result := digit . result
Return result
}
 
parse(number, base)
{
result = 0
pos := StrLen(number) - 1
Loop, Parse, number
{
result := ((base ** pos) * A_LoopField) + result
base -= 1
}
Return result
}

alternate implementation contributed by Laszlo on the ahk forum

MsgBox % ToBase(29,3)
MsgBox % ToBase(255,16)
 
MsgBox % FromBase("100",8)
MsgBox % FromBase("ff",16)
 
ToBase(n,b) { ; n >= 0, 1 < b <= 36
Return (n < b ? "" : ToBase(n//b,b)) . ((d:=mod(n,b)) < 10 ? d : Chr(d+87))
}
 
FromBase(s,b) { ; convert base b number s=strings of 0..9,a..z, to AHK number
Return (L:=StrLen(s))=0 ? "":(L>1 ? FromBase(SubStr(s,1,L-1),b)*b:0) + ((c:=Asc(SubStr(s,0)))>57 ? c-87:c-48)
}

[edit] AWK

function strtol(str, base)
{
symbols = "0123456789abcdefghijklmnopqrstuvwxyz"
res = 0
str = tolower(str)
for(i=1; i < length(str); i++) {
res += index(symbols, substr(str, i, 1)) - 1
res *= base
}
res += index(symbols, substr(str, length(str), 1)) - 1
return res
}
 
function ltostr(num, base)
{
symbols = "0123456789abcdefghijklmnopqrstuvwxyz"
res = ""
do {
res = substr(symbols, num%base + 1, 1) res
num = int(num/base)
} while ( num != 0 )
return res
}
 
BEGIN {
print strtol("7b", 16)
print ltostr(123, 16)
}

[edit] C

#include <stdlib.h>
#include <string.h>
#include <assert.h>
 
#define MAX_OUTSTR_LEN 65
char *to_base(long num, int base)
{
static const char *map = "0123456789abcdefghijklmnopqrstuvwxyz";
char *result = NULL;
int i=0, j;
char t;
 
if ( base > strlen(map) ) return NULL;
 
result = malloc(sizeof(char)*MAX_OUTSTR_LEN);
*result = 0;
do {
result[i++] = map[ num%base ];
num /= base;
} while(num != 0);
result[i] = 0;
for(j=0; j < i/2; j++) { /* invert the rests */
t = result[j];
result[j] = result[i-j-1];
result[i-j-1] = t;
}
return result;
}
 
long from_base(const char *num_str, int base)
{
char *endptr;
int result = strtol(num_str, &endptr, base);
assert(*endptr == '\0'); /* if there are any characters left, then string contained invalid characters */
return result;
/* there is also strtoul() for parsing into an unsigned long */
/* in C99, there is also strtoll() and strtoull() for parsing into long long and unsigned long long, respectively */
}

[edit] C++

#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;
}

[edit] Common 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"

[edit] D

D standard library string module included functions to convert number to string at a radix.

module std.string;
char[] toString(long value, uint radix);
char[] toString(ulong value, uint radix);

Implementation.

module radixstring ;
import std.stdio ;
import std.ctype ;
 
const string Digits = "0123456789abcdefghijklmnopqrstuvwxyz" ;
 
int dtoi(char dc, int radix) {
static int[char] digit ;
char d = tolower(dc) ;
if (digit.length == 0) // not init yet
foreach(i,c ; Digits)
digit[c] = i ;
if (radix > 1 & radix <= digit.length)
if (d in digit)
if (digit[d] < radix)
return digit[d] ;
return int.min ; // a negative for error ;
}
 
ulong AtoI(string str, int radix = 10, int* consumed = null) {
ulong result = 0;
int sp = 0 ;
for(; sp < str.length ; sp++) {
int d = dtoi(str[sp], radix) ;
if (d >= 0) // valid digit char
result = radix*result + d ;
else
break ;
}
if(sp != str.length) // some char in str not converted
sp = -sp ;
if (!(consumed is null)) // signal error if not positive ;
*consumed = sp ;
return result ;
}
 
string ItoA(ulong num, int radix = 10) {
string result = null ;
// if (radix < 2 || radix > Digits.length) throw Error
while (num > 0) {
int d = num % radix ;
result = Digits[d]~ result ;
num = (num - d) / radix ;
}
return result == null ? "0" : result ;
}
 
void main(string[] args) {
string numstr = "1ABcdxyz???" ;
int ate ;
writef("%s (%d) = %d",numstr, 16, AtoI(numstr, 16, &ate)) ;
if(ate <= 0) writefln("\tcheck: %s<%s>",numstr[0..-ate], numstr[-ate..$]) ;
else writefln() ;
writefln(ItoA(60272032366,36)," ",ItoA(591458,36)) ;
}

[edit] E

def stringToInteger := __makeInt
def integerToString(i :int, base :int) {
return i.toString(base)
}
? stringToInteger("200", 16)
# value: 512
 
? integerToString(200, 16)
# value: "c8"

[edit] Factor

USE: math.parser
 
12345 16 >base .
"3039" 16 base> .

[edit] 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.

42 dup
2 base !
. \ 101010
hex
. \ 2A
decimal

Many variants of Forth support literals in some bases, such as hex, using a prefix

$ff .   \ 255

[edit] Fortran

Works with: Fortran version 90 and later

MODULE Conversion
IMPLICIT NONE
CHARACTER(36) :: alphanum = "0123456789abcdefghijklmnopqrstuvwxyz"
 
CONTAINS
 
FUNCTION ToDecimal(base, instr)
INTEGER :: ToDecimal
INTEGER :: length, i, n, base
CHARACTER(*) :: instr
 
ToDecimal = 0
length = LEN(instr)
DO i = 1, length
n = INDEX(alphanum, instr(i:i)) - 1
n = n * base**(length-i)
Todecimal = ToDecimal + n
END DO
END FUNCTION ToDecimal
 
FUNCTION ToBase(base, number)
CHARACTER(31) :: ToBase
INTEGER :: base, number, i, rem
 
ToBase = " "
DO i = 31, 1, -1
IF(number < base) THEN
ToBase(i:i) = alphanum(number+1:number+1)
EXIT
END IF
rem = MOD(number, base)
ToBase(i:i) = alphanum(rem+1:rem+1)
number = number / base
END DO
ToBase = ADJUSTL(ToBase)
END FUNCTION ToBase
 
END MODULE Conversion
 
PROGRAM Base_Convert
USE Conversion
 
WRITE (*,*) ToDecimal(16, "1a")
WRITE (*,*) ToBase(16, 26)
 
END PROGRAM

[edit] Groovy

Solution:

def radixParse = { s, radix -> Integer.parseInt(s, radix) }
def radixFormat = { i, radix -> Integer.toString(i, radix) }

Test Program:

def numString = '101'
(2..Character.MAX_RADIX).each { radix ->
def value = radixParse(numString, radix)
assert value == radix**2 + 1
printf ("  %3s (%2d) == %4d (10)\n", numString, radix, value)
 
def valM2str = radixFormat(value - 2, radix)
def biggestDigit = radixFormat(radix - 1, radix)
assert valM2str == biggestDigit + biggestDigit
printf ("%3s (%2d) - 2 (10) == %4s (%2d)\n", numString, radix, valM2str, radix)
}

Output:

         101 ( 2) ==    5 (10)
101 ( 2) - 2 (10) ==   11 ( 2)
         101 ( 3) ==   10 (10)
101 ( 3) - 2 (10) ==   22 ( 3)
         101 ( 4) ==   17 (10)
101 ( 4) - 2 (10) ==   33 ( 4)
         101 ( 5) ==   26 (10)
101 ( 5) - 2 (10) ==   44 ( 5)
         101 ( 6) ==   37 (10)
101 ( 6) - 2 (10) ==   55 ( 6)
         101 ( 7) ==   50 (10)
101 ( 7) - 2 (10) ==   66 ( 7)
         101 ( 8) ==   65 (10)
101 ( 8) - 2 (10) ==   77 ( 8)
         101 ( 9) ==   82 (10)
101 ( 9) - 2 (10) ==   88 ( 9)
         101 (10) ==  101 (10)
101 (10) - 2 (10) ==   99 (10)
         101 (11) ==  122 (10)
101 (11) - 2 (10) ==   aa (11)
         101 (12) ==  145 (10)
101 (12) - 2 (10) ==   bb (12)
         101 (13) ==  170 (10)
101 (13) - 2 (10) ==   cc (13)
         101 (14) ==  197 (10)
101 (14) - 2 (10) ==   dd (14)
         101 (15) ==  226 (10)
101 (15) - 2 (10) ==   ee (15)
         101 (16) ==  257 (10)
101 (16) - 2 (10) ==   ff (16)
         101 (17) ==  290 (10)
101 (17) - 2 (10) ==   gg (17)
         101 (18) ==  325 (10)
101 (18) - 2 (10) ==   hh (18)
         101 (19) ==  362 (10)
101 (19) - 2 (10) ==   ii (19)
         101 (20) ==  401 (10)
101 (20) - 2 (10) ==   jj (20)
         101 (21) ==  442 (10)
101 (21) - 2 (10) ==   kk (21)
         101 (22) ==  485 (10)
101 (22) - 2 (10) ==   ll (22)
         101 (23) ==  530 (10)
101 (23) - 2 (10) ==   mm (23)
         101 (24) ==  577 (10)
101 (24) - 2 (10) ==   nn (24)
         101 (25) ==  626 (10)
101 (25) - 2 (10) ==   oo (25)
         101 (26) ==  677 (10)
101 (26) - 2 (10) ==   pp (26)
         101 (27) ==  730 (10)
101 (27) - 2 (10) ==   qq (27)
         101 (28) ==  785 (10)
101 (28) - 2 (10) ==   rr (28)
         101 (29) ==  842 (10)
101 (29) - 2 (10) ==   ss (29)
         101 (30) ==  901 (10)
101 (30) - 2 (10) ==   tt (30)
         101 (31) ==  962 (10)
101 (31) - 2 (10) ==   uu (31)
         101 (32) == 1025 (10)
101 (32) - 2 (10) ==   vv (32)
         101 (33) == 1090 (10)
101 (33) - 2 (10) ==   ww (33)
         101 (34) == 1157 (10)
101 (34) - 2 (10) ==   xx (34)
         101 (35) == 1226 (10)
101 (35) - 2 (10) ==   yy (35)
         101 (36) == 1297 (10)
101 (36) - 2 (10) ==   zz (36)

[edit] Haskell

It's actually more useful to represent digits internally as numbers instead of characters, because then one can define operations that work directly on this representation.

So conversion to and from digits represented as 0-9 and a-z is done in an additional step.

import Data.List
import Data.Char
 
toBase :: Int -> Int -> [Int]
toBase b v = toBase' [] v where
toBase' a 0 = a
toBase' a v = toBase' (r:a) q where (q,r) = v `divMod` b
 
fromBase :: Int -> [Int] -> Int
fromBase b ds = foldl' (\n k -> n * b + k) 0 ds
 
toAlphaDigits :: [Int] -> String
toAlphaDigits = map convert where
convert n | n < 10 = chr (n + ord '0')
| otherwise = chr (n + ord 'a' - 10)
 
fromAlphaDigits :: String -> [Int]
fromAlphaDigits = map convert where
convert c | isDigit c = ord c - ord '0'
| isUpper c = ord c - ord 'A' + 10
| isLower c = ord c - ord 'a' + 10

Example:

*Main> toAlphaDigits $ toBase 16 $ 42
"2a"
*Main> fromBase 16 $ fromAlphaDigits $ "2a"
42

[edit] J

J supports direct specification of numbers by base, as in these examples:

   2b100 8b100 10b100 16b100 36b100 36bzy
4 64 100 256 1296 1294

Programs for conversion of numeric values to literals, and of literals to numbers:

numerals=: '0123456789abcdefghijklmnopqrstuvwxyz'
baseNtoL=: numerals {~ #.inv
baseLtoN=: [ #. numerals i. ]

Examples of use:

   2 baseNtoL 100 101
1100100
1100101
16 baseNtoL 26
1a
36 baseLtoN 'zy'
1294

These may be combined so the conversion performed is derived from the type of argument received.

   base=: baseNtoL :: baseLtoN
 
16 base 'aa'
170
16 base 170
aa

See also primary verbs Base and Antibase.

[edit] Java

for long's:

public static long backToTen(String num, int oldBase){
return Long.parseLong(num, oldBase); //takes both uppercase and lowercase letters
}
 
public static String tenToBase(long num, int newBase){
return Long.toString(num, newBase);//add .toUpperCase() for capital letters
}

for BigInteger's:

public static BigInteger backToTenBig(String num, int oldBase){
return new BigInteger(num, oldBase); //takes both uppercase and lowercase letters
}
 
public static String tenBigToBase(BigInteger num, int newBase){
return num.toString(newBase);//add .toUpperCase() for capital letters
}

[edit] 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.

[edit] M4

eval(26,16)
define(`frombase',`eval(0r$2:$1)')
frombase(1a,16)

Output:

1a

26

[edit] OCaml

let int_of_basen n str =
match n with
| 16 -> int_of_string("0x" ^ str)
| 2 -> int_of_string("0b" ^ str)
| 8 -> int_of_string("0o" ^ str)
| _ -> failwith "unhandled"
 
let basen_of_int n d =
match n with
| 16 -> Printf.sprintf "%x" d
| 8 -> Printf.sprintf "%o" d
| _ -> failwith "unhandled"
# basen_of_int 16 26 ;;
- : string = "1a"

# int_of_basen 16 "1a" ;;
- : int = 26

A real base conversion example: Translation of: Haskell

let to_base b v =
let rec to_base' a v =
if v = 0 then
a
else
to_base' (v mod b :: a) (v / b)
in
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

Example:

# to_alpha_digits (to_base 16 42);;
- : string = "2a"
# from_base 16 (from_alpha_digits "2a");;
- : int = 42

[edit] Perl

To parse from a base:

use POSIX;
 
my ($num, $n_unparsed) = strtol('1a', 16);
$n_unparsed == 0 or die "invalid characters found";
print "$num\n"; # prints "26"

To format into a base:

sub digitize
# Converts an integer to a single digit.
{my $i = shift;
$i < 10
? $i
: ('a' .. 'z')[$i - 10];}
 
sub to_base
{my ($int, $radix) = @_;
my $numeral = '';
do {
$numeral .= digitize($int % $radix);
} while $int = int($int / $radix);
scalar reverse $numeral;}

[edit] PHP

PHP has a base_convert() function that directly converts between strings of one base and strings of another base:

base_convert("26", 10, 16); // returns "1a"

If you want to convert a string to an integer, the intval() function optionally takes a base argument when given a string:

intval("1a", 16); // returns 26

To go the other way around, I guess you can use base_convert() again; I am unaware of a better way:

base_convert(26, 10, 16); // returns "1a"

In addition, there are specialized functions for converting certain bases:

// converts int to binary string
decbin(26); // returns "11010"
// converts int to octal string
decoct(26); // returns "32"
// converts int to hex string
dechex(26); // returns "1a"
// converts binary string to int
bindec("11010"); // returns 26
// converts octal string to int
octdec("32"); // returns 26
// converts hex string to int
hexdec("1a"); // returns 26

[edit] 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;
 

[edit] Pop11

Pop11 can input and output routines can use any base up to 36 (depending on value 'pop_pr_radix' variable). 'radix_apply' runs i/o routine temporarly setting 'pop_pr_radix' to given value. 'sprintf' procedure instead of printing returns string. So, to convert number to given value we just compose built-in procedures:

define number_to_base(n, base);
radix_apply(n, '%p', sprintf, base);
enddefine;

In input base optionally preceeds the number, for example 8:15 is 13. So, to convert string in given base we need to prepend base prefix and read number from string:

define string_in_base_to_number(s, base);
incharitem(stringin(base >< ':' >< s))();
enddefine;

[edit] Python

def baseN(num,b):
return ((num == 0) and "0" ) or ( baseN(num // b, b).lstrip("0") + "0123456789abcdefghijklmnopqrstuvwxyz"[num % b])
 
# alternatively:
def baseN(num,b):
if num == 0: return "0"
result = ""
while num != 0:
num, d = divmod(num, b)
result += "0123456789abcdefghijklmnopqrstuvwxyz"[d]
return result[::-1] # reverse
 
k = 26
s = baseN(k,16) # returns the string 1a
i = int('1a',16) # returns the integer 26

[edit] Ruby

s = 26.to_s(16) # returns the string 1a
i = '1a'.to_i(16) # returns the integer 26

Caution: to_i simply stops when it reaches an invalid character; it does not raise any exceptions. So sometimes it may appear to parse something and get a result, but it is only based on part of the input string:

"59".to_i(7) # returns 5, but this is probably not what you wanted

For a general base parsing that raises an exception for invalid characters:

Translation of: Tcl

module BaseConvert
DIGITS = %w{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}
 
def baseconvert(str, basefrom, baseto)
dec2base(base2dec(str, basefrom), baseto)
end
 
def base2dec(str, base)
raise ArgumentError, "base is invalid" unless base.between?(2, DIGITS.length)
res = 0
str.to_s.downcase.each_char do |c|
idx = DIGITS[0,base].find_index(c)
idx.nil? and raise ArgumentError, "invalid base-#{base} digit: #{c}"
res = res * base + idx
end
res
end
 
def dec2base(n, base)
return "0" if n == 0
raise ArgumentError, "base is invalid" unless base.between?(2, DIGITS.length)
res = []
while n > 0
n, r = n.divmod(base)
res.unshift(DIGITS[r])
end
res.join("")
end
end
 
include BaseConvert
p baseconvert("107h", 23, 7) # => "50664"

[edit] Slate

26 printString &radix: 16
Integer readFrom: '1A' &radix: 16.

[edit] Standard ML

Translation of: Haskell

fun toBase b v = let
fun toBase' (a, 0) = a
| toBase' (a, v) = toBase' (v mod b :: a, v div b)
in
toBase' ([], v)
end
 
fun fromBase b ds =
foldl (fn (k, n) => n * b + k) 0 ds
 
val toAlphaDigits = let
fun convert n = if n < 10 then chr (n + ord #"0")
else chr (n + ord #"a" - 10)
in
implode o map convert
end
 
val fromAlphaDigits = let
fun convert c = if Char.isDigit c then ord c - ord #"0"
else if Char.isUpper c then ord c - ord #"A" + 10
else if Char.isLower c then ord c - ord #"a" + 10
else raise Match
in
map convert o explode
end

Example:

val toAlphaDigits = fn : int list -> string
- toAlphaDigits (toBase 16 42);
val it = "2a" : string
- fromBase 16 (fromAlphaDigits "2a");
val it = 42 : int

[edit] Tcl

Tcl scan and format commands can convert between decimal, octal and hexadecimal, but this solution can convert between any arbitrary bases.

namespace eval baseconvert {
variable chars "0123456789abcdefghijklmnopqrstuvwxyz"
namespace export baseconvert
}
proc baseconvert::dec2base {n b} {
variable chars
expr {$n == 0 ? 0
 : "[string trimleft [dec2base [expr {$n/$b}] $b] 0][string index $chars [expr {$n%$b}]]"
}
}
proc baseconvert::base2dec {n b} {
variable chars
set sum 0
foreach char [split $n ""] {
set d [string first $char [string range $chars 0 [expr {$b - 1}]]]
if {$d == -1} {error "invalid base-$b digit '$char' in $n"}
set sum [expr {$sum * $b + $d}]
}
return $sum
}
proc baseconvert::baseconvert {n basefrom baseto} {
dec2base [base2dec $n $basefrom] $baseto
}
 
namespace import baseconvert::baseconvert
baseconvert 12345 10 23 ;# ==> 107h
baseconvert 107h 23 7 ;# ==> 50664
baseconvert 50664 7 10 ;# ==> 12345

[edit] Ursala

A function parameterized by the base b performs the conversion in each direction. Folding (=>), iteration (->), and reification (-:) operators among others are helpful.

#import std
#import nat
 
num_to_string "b" = ||'0'! (-: num digits--letters)*+ @NiX ~&r->l ^|rrPlCrlPX/~& division\"b"
 
string_to_num "b" = @x =>0 sum^|/(-:@rlXS num digits--letters) product/"b"

This test program performs the conversions in both directions for a selection of numbers in base 8 and base 32.

test_data = <1,2,15,32,100,65536,323498993>
 
#cast %sLnLUL
 
tests =
 
<
num_to_string32* test_data,
string_to_num32* num_to_string32* test_data,
num_to_string8* test_data,
string_to_num8* num_to_string8* test_data>

output:

<
   <'1','2','f','10','34','2000','9kgcvh'>,
   <1,2,15,32,100,65536,323498993>,
   <'1','2','17','40','144','200000','2322031761'>,
   <1,2,15,32,100,65536,323498993>>
Personal tools
Google AdSense