Non-decimal radices/Convert: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 64: | Line 64: | ||
end Number_Base_Conversion;</lang> |
end Number_Base_Conversion;</lang> |
||
=={{header|ALGOL 68}}== |
|||
{{trans|python}} |
|||
{{works with|ALGOL 68|Standard - no extensions to language used}} |
|||
{{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 algol> |
|||
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</lang> |
|||
Output: |
|||
<pre> |
|||
-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 |
|||
</pre> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
<lang cpp>#include <limits> |
<lang cpp>#include <limits> |