Non-decimal radices/Convert: Difference between revisions
Content added Content deleted
m (→{{header|C}}: a long of 256 bits does not exist still) |
(awk) |
||
Line 155: | Line 155: | ||
===Other libraries or implementation specific extensions=== |
===Other libraries or implementation specific extensions=== |
||
As of February 2009 no open source libraries to do this task have been located. |
As of February 2009 no open source libraries to do this task have been located. |
||
=={{header|AWK}}== |
|||
<lang awk>function strtol(str, base) |
|||
{ |
|||
symbols = "0123456789abcdefghijklmnopqrstuvwxyz" |
|||
res = 0 |
|||
str = tolower(str) |
|||
for(i=1; i < length(str); i++) { |
|||
res += index(symbols, substr(str, i, 1)) - 1 |
|||
res *= base |
|||
} |
|||
res += index(symbols, substr(str, length(str), 1)) - 1 |
|||
return res |
|||
} |
|||
function ltostr(num, base) |
|||
{ |
|||
symbols = "0123456789abcdefghijklmnopqrstuvwxyz" |
|||
res = "" |
|||
do { |
|||
res = substr(symbols, num%base + 1, 1) res |
|||
num = int(num/base) |
|||
} while ( num != 0 ) |
|||
return res |
|||
} |
|||
BEGIN { |
|||
print strtol("7b", 16) |
|||
print ltostr(123, 16) |
|||
}</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |