Anonymous user
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
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 % parse(200, 16) ; 512
Line 182 ⟶ 180:
}
Return result
</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.
Many variants of Forth support literals in some bases, such as hex, using a prefix
=={{header|Fortran}}==
{{Works with|Fortran|90 and later}}
<lang fortran>
CONTAINS▼
FUNCTION ToDecimal(base, instr)▼
INTEGER :: ToDecimal▼
INTEGER :: length, i, n, base▼
ToDecimal = 0▼
DO
n = n * base**(length-i)
END DO▼
END FUNCTION
▲ CHARACTER(31) :: ToBase
FUNCTION
CHARACTER(31) :: ToBase
ToBase = " "▼
DO
IF(number < base)
END IF▼
▲ ToBase(i:i) = alphanum(rem+1:rem+1)
ToBase(i:i) = alphanum(rem+1:rem+1)
ToBase = ADJUSTL(ToBase)
END FUNCTION ToBase
END MODULE Conversion▼
PROGRAM Base_Convert▼
USE Conversion▼
WRITE (*,*) ToDecimal(16, "1a")▼
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▼
▲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>
Example:
<lang haskell>*Main> toAlphaDigits $ toBase 16 $ 42▼
▲*Main> toAlphaDigits $ toBase 16 $ 42
"2a"
*Main> fromBase 16 $ fromAlphaDigits $ "2a"
42</lang>
=={{header|J}}==
J supports direct specification of numbers by base, as in these examples:
<lang j> 2b100 8b100 10b100 16b100 36b100 36bzy
Programs for conversion of numeric values to literals, and of literals to numbers:
Examples of use:
<lang j> 2 baseNtoL 100 101
These may be combined so the conversion performed is derived from the type of argument received.
<lang j> base=: baseNtoL :: baseLtoN
▲aa</lang>
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)
define(`frombase',`eval(0r$2:$1)')
frombase(1a,16)</lang>
Output:
Line 757 ⟶ 748:
built-in procedures:
<lang pop11>define number_to_base(n, base);▼
▲define number_to_base(n, base);
radix_apply(n, '%p', sprintf, base);
enddefine;</lang>
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);▼
▲define string_in_base_to_number(s, base);
incharitem(stringin(base >< ':' >< s))();
enddefine;</lang>
=={{header|Python}}==
Line 830 ⟶ 817:
include BaseConvert
p baseconvert("107h", 23, 7) # => "50664"</lang>
=={{header|Slate}}==
<lang slate>26 printString &radix: 16
=={{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 nat
|