Non-decimal radices/Convert: Difference between revisions
Content added Content deleted
m (ββOne Implementation: fixed pre closing tag) |
(Added BBC BASIC) |
||
Line 262: | Line 262: | ||
print ltostr(123, 16) |
print ltostr(123, 16) |
||
}</lang> |
}</lang> |
||
=={{header|BBC BASIC}}== |
|||
<lang bbcbasic> PRINT " 0 (decimal) -> " FNtobase(0, 16) " (base 16)" |
|||
PRINT " 26 (decimal) -> " FNtobase(26, 16) " (base 16)" |
|||
PRINT "383 (decimal) -> " FNtobase(383, 16) " (base 16)" |
|||
PRINT " 26 (decimal) -> " FNtobase(26, 2) " (base 2)" |
|||
PRINT "383 (decimal) -> " FNtobase(383, 2) " (base 2)" |
|||
PRINT " 1a (base 16) -> " ;FNfrombase("1a", 16) " (decimal)" |
|||
PRINT " 1A (base 16) -> " ;FNfrombase("1A", 16) " (decimal)" |
|||
PRINT "17f (base 16) -> " ;FNfrombase("17f", 16) " (decimal)" |
|||
PRINT "101111111 (base 2) -> " ;FNfrombase("101111111", 2) " (decimal)" |
|||
END |
|||
DEF FNtobase(N%, B%) |
|||
LOCAL D%,A$ |
|||
REPEAT |
|||
D% = N% MOD B% |
|||
N% DIV= B% |
|||
A$ = CHR$(48 + D% - 39*(D%>9)) + A$ |
|||
UNTIL N% = FALSE |
|||
=A$ |
|||
DEF FNfrombase(A$, B%) |
|||
LOCAL N% |
|||
REPEAT |
|||
N% *= B% |
|||
N% += ASC(A$) - 48 + 7*(ASCA$>64) + 32*(ASCA$>96) |
|||
A$ = MID$(A$,2) |
|||
UNTIL A$ = "" |
|||
= N%</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> |
|||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |