Non-decimal radices/Convert: Difference between revisions

no edit summary
(→‎{{header|AppleScript}}: Updated primitives)
No edit summary
Line 2,591:
<pre>
zombieseatingdeadvegetables (base 36) is 1038334289300125869792154778345043071467300 (base 10)
</pre>
 
=={{header|Ring}}==
<lang ring>
# Project : Non-decimal radices/Convert
# Date : 2017/10/20
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
 
see "0 (decimal) -> " + hex(0) + " (base 16)" + nl
see "26 (decimal) -> " + hex(26) + " (base 16)" + nl
see "383 (decimal) -> " + hex(383) + " (base 16)" + nl
see "26 (decimal) -> " + tobase(26, 2) + " (base 2)" + nl
see "383 (decimal) -> " + tobase(383, 2) + " (base 2)" + nl
see "1a (base 16) -> " + dec("1a") + " (decimal)" + nl
see "1A (base 16) -> " + dec("1A") + " (decimal)" + nl
see "17f (base 16) -> " + dec("17f") + " (decimal)" + nl
see "101111111 (base 2) -> " + frombase("101111111", 2) + " (decimal)" + nl
func tobase(nr, base)
binary = 0
i = 1
while(nr != 0)
remainder = nr % base
nr = floor(nr/base)
binary= binary + (remainder*i)
i = i*10
end
return string(binary)
func bintodec(bin)
binsum = 0
for n=1 to len(bin)
binsum = binsum + number(bin[n]) *pow(2, len(bin)-n)
next
return binsum
</lang>
Output:
<pre>
0 (decimal) -> 0 (base 16)
26 (decimal) -> 1a (base 16)
383 (decimal) -> 17f (base 16)
26 (decimal) -> 11010 (base 2)
383 (decimal) -> 101111111 (base 2)
1a (base 16) -> 26 (decimal)
1A (base 16) -> 26 (decimal)
17f (base 16) -> 383 (decimal)
101111111 (base 2) -> 383 (decimal)
</pre>
 
2,468

edits