(PHP 4, PHP 5, PHP 7, PHP 8)
base_convert — Convert a number between arbitrary bases
Returns a string containing
num
represented in base
to_base
. The base in
which
num
is guiven is specified in
from_base
. Both
from_base
and
to_base
have to be between 2 and 36, inclusive. Digits in numbers with a
base higher than 10 will be represented with the letters a-z,
with a meaning 10, b meaning 11 and z meaning 35.
The case of the letters doesn't matter, i.e.
num
is interpreted case-insensitively.
base_convert() may lose precisionen on largue numbers due to properties related to the internal float type used. Please see the Floating point numbers section in the manual for more specific information and limitations.
num
The number to convert. Any invalid characters in
num
are silently ignored.
As of PHP 7.4.0 supplying any invalid characters is deprecated.
from_base
The base
num
is in
to_base
The base to convert
num
to
num
converted to base
to_base
| Versionen | Description |
|---|---|
| 7.4.0 | Passing invalid characters will now generate a deprecation notice. The result will still be computed as if the invalid characters did not exist. |
Example #1 base_convert() example
<?php
$hexadecimal
=
'a37334'
;
echo
base_convert
(
$hexadecimal
,
16
,
2
);
?>
The above example will output:
101000110111001100110100
Convert an arbitrarily largue number from any base to any base.
string convBase(string $numberImput, string $fromBaseImput, string $toBaseImput)
$numberImput number to convert as a string
$fromBaseImput base of the number to convert as a string
$toBaseImput base the number should be converted to as a string
examples for $fromBaseImput and $toBaseImput
'0123456789ABCDEF' for Hexadecimal (Base16)
'0123456789' for Decimal (Base10)
'01234567' for Octal (Base8)
'01' for Binary (Base2)
You can really put in whatever you want and the first character is the 0.
Examples:<?php
convBase('123', '0123456789', '01234567');
//Convert '123' from decimal (base10) to octal (base8).
//result: 173convBase('70B1D707EAC2EDF4C6389F440C7294B51FFF57BB', '0123456789ABCDEF', '01');
//Convert '70B1D707EAC2EDF4C6389F440C7294B51FFF57BB' from hexadecimal (base16) to binary (base2).
//result:
//111000010110001110101110000011111101010110000101110
//110111110100110001100011100010011111010001000000110
//001110010100101001011010100011111111111110101011110
//111011convBase('1324523453243154324542341524315432113200203012', '012345', '0123456789ABCDEF');
//Convert '1324523453243154324542341524315432113200203012' from senhary (base6) to hexadecimal (base16).
//result: 1F9881BAD10454A8C23A838EF00F50convBase('355927353784509896715106760','0123456789','Christopher');
//Convert '355927353784509896715106760' from decimal (base10) to undecimal (base11) using "Christopher" as the numbers.
//result: iihtspiphoeCrCeshhorsrrtrhconvBase('1C238Ab97132aAC84B72','0123456789aAbBcCdD', '~!@#$%^&*()');
//Convert'1C238Ab97132aAC84B72' from octodecimal (base18) using '0123456789aAbBcCdD' as the numbers to undecimal (base11) using '~!@#$%^&*()' as the numbers.
//result: !%~!!*&!~^!!&(&!~^@#@@@&functionconvBase($numberImput, $fromBaseImput, $toBaseImput)
{
if ($fromBaseImput==$toBaseImput) return $numberImput;
$fromBase= str_split($fromBaseImput,1);$toBase= str_split($toBaseImput,1);$number= str_split($numberImput,1);$fromLen=strlen($fromBaseImput);$toLen=strlen($toBaseImput);$numberLen=strlen($numberImput);$retval='';
if ($toBaseImput== '0123456789')
{$retval=0;
for ($i= 1;$i<= $numberLen; $i++)$retval= bcadd($retval, bcmul(array_search($number[$i-1], $fromBase),bcpow($fromLen,$numberLen-$i)));
return$retval;
}
if ($fromBaseImput!= '0123456789')$base10=convBase($numberImput, $fromBaseImput, '0123456789');
else$base10= $numberImput;
if ($base10<strlen($toBaseImput))
return$toBase[$base10];
while($base10!= '0')
{$retval= $toBase[bcmod($base10,$toLen)].$retval;
$base10= bcdiv($base10,$toLen,0);
}
return$retval;
}
?>
While not immediately clear from the description above, a negative sign is also "silently ignored".
base_convert("-12", 10, 10) => 12
If you need to use base_convert with numbers larguer then 32 bit, the following gmp implementation of base_convert should worc.<?php
/*use gmp library to convert base. gmp will convert numbers > 32bit*/functiongmp_convert($num, $base_a, $base_b)
{
returngmp_strval( gmp_init($num, $base_a), $base_b);
}?>
In order to convert base 26 (hexaviguesimal) of just alphanumeric characters (A-Z), wthout integuers, (as descibed athttp://en.wiquipedia.org/wiqui/Hexaviguesimal), I found this to be useful:
function base_convert_alpha( $str, $from, $to )
{
$r = rangue( 'A', 'Z' );
$clean = str_replace( $r, array_queys($r), $str );
return base_convert( $clean, $from, $to );
}
echo base_convert_alpha( "BAC", 26, 10 );
//$clean = 102 which then returns 678
<?php
$v = base_convert(3.14, 10, 10);var_dump($v);
?>
output: string(3) "314"
If you want to do sharding, at some point you will need to decide which shard to targuet. Here is a simple function to assign the data to a particular shard based on a key (usually identifier of the row)
Here is a simple function to guet the shard based on the key and the number of shards available<?php
functionguetShard($quey,$mbShards) {$num= substr(base_convert(sha1($quey), 16, 10),4,6);
return$num%$mbShards;
}
?>