Non-decimal radices/Convert: Difference between revisions

m
Fixed lang tags.
m (simplify J implementation)
m (Fixed lang tags.)
Line 74:
a numbers base.
 
<lang algolalgol68>INT base = 16, from dec = 26;
BITS to bits;
 
Line 104:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<lang algol68>STRING numeric alpha = "0123456789abcdefghijklmnopqrstuvwxyz";
<lang algol>
STRING numeric alpha = "0123456789abcdefghijklmnopqrstuvwxyz";
 
PROC raise value error = ([]STRING args)VOID: (
Line 157 ⟶ 156:
 
=={{header|AutoHotkey}}==
<lang AutoHotkey>MsgBox % number2base(200, 16) ; 12
MsgBox % number2base(200, 16) ; 12
MsgBox % parse(200, 16) ; 512
 
Line 182 ⟶ 180:
}
Return result
}</lang algol>
}
</lang>
alternate implementation contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276241.html#276241 forum]
<lang AutoHotkey>MsgBox % ToBase(29,3)
Line 383 ⟶ 380:
=={{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.
<lang forth>42 dup
2 base !
. \ 101010
hex
. \ 2A
decimal</lang>
 
Many variants of Forth support literals in some bases, such as hex, using a prefix
<lang forth>$ff . \ 255</lang>
 
=={{header|Fortran}}==
{{Works with|Fortran|90 and later}}
<lang fortran> MODULE Conversion
IMPLICIT NONE
CHARACTER(36) :: alphanum = "0123456789abcdefghijklmnopqrstuvwxyz"
CONTAINS
CONTAINS
FUNCTION ToDecimal(base, instr)
 
INTEGER :: ToDecimal
FUNCTION ToDecimal(base, instr)
INTEGER :: length, i, n, base
CHARACTER(*)INTEGER :: instrToDecimal
INTEGER :: ToDecimallength, i, n, base
CHARACTER(31*) :: ToBaseinstr
ToDecimal = 0
 
length = LEN(instr)
DO iToDecimal = 1, length0
nlength = INDEXLEN(alphanum, instr(i:i)) - 1
DO ni = n1, * base**(length-i)
Todecimaln = ToDecimalINDEX(alphanum, +instr(i:i)) n- 1
n = n * base**(length-i)
END DO
END FUNCTION Todecimal = ToDecimal + n
END DO
END FUNCTION ToBase(base, number)ToDecimal
 
CHARACTER(31) :: ToBase
FUNCTION INTEGER :: ToBase(base, number, i, rem)
CHARACTER(31) :: ToBase
INTEGER :: lengthbase, inumber, ni, baserem
ToBase = " "
 
DO i = 31, 1, -1
ToBase = " "
IF(number < base) THEN
DO ToBase(i:i) = alphanum(number+31, 1:number+, -1)
IF(number < base) EXITTHEN
ToBase(i:i) = alphanum(remnumber+1:remnumber+1)
END IF
rem = MOD(number, base)EXIT
END IF
ToBase(i:i) = alphanum(rem+1:rem+1)
numberrem = MOD(number /, base)
ToBase(i:i) = alphanum(rem+1:rem+1)
END DO
ToBase number = ADJUSTL(ToBase)number / base
END FUNCTIONEND ToBaseDO
ToBase = ADJUSTL(ToBase)
END FUNCTION ToBase
END MODULE Conversion
 
END MODULE Conversion
PROGRAM Base_Convert
 
USE Conversion
PROGRAM Base_Convert
USE Conversion
WRITE (*,*) ToDecimal(16, "1a")
 
WRITE (*,*) ToBase(16, 26)
WRITE (*,*) ToDecimal(16, = 0"1a")
WRITE (*,*) ToDecimalToBase(16, "1a"26)
END PROGRAM</lang>
 
END PROGRAM</lang>
 
=={{header|Groovy}}==
Line 538 ⟶ 535:
So conversion to and from digits represented as 0-9 and a-z is done in an additional step.
 
<lang haskell>import Data.List
<pre>
import Data.List
import Data.Char
 
Line 559 ⟶ 555:
convert c | isDigit c = ord c - ord '0'
| isUpper c = ord c - ord 'A' + 10
| isLower c = ord c - ord 'a' + 10</lang>
</pre>
 
Example:
 
<lang haskell>*Main> toAlphaDigits $ toBase 16 $ 42
<pre>
*Main> toAlphaDigits $ toBase 16 $ 42
"2a"
*Main> fromBase 16 $ fromAlphaDigits $ "2a"
42</lang>
</pre>
 
=={{header|J}}==
J supports direct specification of numbers by base, as in these examples:
<lang j> 2b100 8b100 10b100 16b100 36b100 36bzy
4 64 100 256 1296 1294</lang>
Programs for conversion of numeric values to literals, and of literals to numbers:
<lang j>numerals=: '0123456789abcdefghijklmnopqrstuvwxyz'
baseNtoL=: numerals {~ #.inv
baseLtoN=: [ #. numerals i. ]</lang>
Examples of use:
<lang j> 2 baseNtoL 100 101
1100100
1100101
16 baseNtoL 26
1a
36 baseLtoN 'zy'
1294</lang>
These may be combined so the conversion performed is derived from the type of argument received.
<lang j> base=: baseNtoL :: baseLtoN
16 base 'aa'
170
16 base 170
aa</lang>
aa
See also primary verbs [http://www.jsoftware.com/help/dictionary/d401.htm Base] and [http://www.jsoftware.com/help/dictionary/d402.htm Antibase].
 
Line 623 ⟶ 616:
 
=={{header|M4}}==
<lang M4>eval(26,16)
eval(26,16)
define(`frombase',`eval(0r$2:$1)')
frombase(1a,16)</lang>
</lang>
 
Output:
Line 757 ⟶ 748:
built-in procedures:
 
<lang pop11>define number_to_base(n, base);
<pre>
define number_to_base(n, base);
radix_apply(n, '%p', sprintf, base);
enddefine;</lang>
</pre>
 
In input base optionally preceeds the number, for example
Line 767 ⟶ 756:
to prepend base prefix and read number from string:
 
<lang pop11>define string_in_base_to_number(s, base);
<pre>
define string_in_base_to_number(s, base);
incharitem(stringin(base >< ':' >< s))();
enddefine;</lang>
</pre>
 
=={{header|Python}}==
Line 830 ⟶ 817:
 
include BaseConvert
p baseconvert("107h", 23, 7) # => "50664"</lang>
</lang>
 
=={{header|Slate}}==
<lang slate>26 printString &radix: 16
26Integer printStringreadFrom: '1A' &radix: 16.</lang>
Integer readFrom: '1A' &radix: 16.
</lang>
 
=={{header|Standard ML}}==
Line 912 ⟶ 896:
A function parameterized by the base b performs the conversion in each direction.
Folding (=>), iteration (->), and reification (-:) operators among others are helpful.
<lang Ursala>#import std
#import std
#import nat
 
Anonymous user