Non-decimal radices/Convert: Difference between revisions
Content added Content deleted
(Added an Algol W sample) |
|||
Line 1,702: | Line 1,702: | ||
}</lang> |
}</lang> |
||
These work on any real type including integer types. |
These work on any real type including integer types. |
||
=={{header|Phix}}== |
|||
<lang Phix>function to_base(integer i, integer base) |
|||
integer c |
|||
sequence s = "" |
|||
while i>0 do |
|||
c = remainder(i,base) |
|||
if c<10 then |
|||
c += '0' |
|||
else |
|||
c += 'a'-10 |
|||
end if |
|||
s = prepend(s,c) |
|||
i = floor(i/base) |
|||
end while |
|||
if length(s) = 0 then |
|||
s = "0" |
|||
end if |
|||
return s |
|||
end function |
|||
function from_base(string s, integer base) |
|||
integer res = 0, c |
|||
for i=1 to length(s) do |
|||
c = s[i] |
|||
if c>='0' and c<='9' then |
|||
c -= '0' |
|||
else |
|||
c -= 'a'-10 |
|||
end if |
|||
res = res*base+c |
|||
end for |
|||
return res |
|||
end function |
|||
?to_base(256,16) |
|||
?from_base("100",16)</lang> |
|||
{{out}} |
|||
<pre> |
|||
"100" |
|||
256 |
|||
</pre> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |