Non-decimal radices/Convert: Difference between revisions

added php
(added php)
Line 295:
{$int += $radix**$n * undigitize substr($numeral, 0, 1, '');}
return $int;}</perl>
 
=={{header|PHP}}==
Python has a base_convert() function that directly converts between strings of one base and strings of another base:
<php>base_convert("26", 10, 16); // returns "1a"</php>
 
If you want to convert a string to an integer, the intval() function optionally takes a base argument when given a string:
<php>intval("1a", 16); // returns 26</php>
 
To go the other way around, I guess you can use base_convert() again; I am unaware of a better way:
<php>base_convert(26, 10, 16); // returns "1a"</php>
 
In addition, there are specialized functions for converting certain bases:
<php>// converts int to binary string
decbin(26); // returns "11010"
// converts int to octal string
decoct(26); // returns "32"
// converts int to hex string
dechex(26); // returns "1a"
// converts int to binary string
bindec("11010"); // returns 26
// converts int to octal string
octdec("32"); // returns 26
// converts int to hex string
hexdec("1a"); // returns 26</php>
 
=={{header|Pop11}}==
Anonymous user