base_convert

(PHP 4, PHP 5, PHP 7, PHP 8)

base_convert β€” ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Ρ‹Π²Π°Π΅Ρ‚ числа ΠΌΠ΅ΠΆΠ΄Ρƒ ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ»ΡŒΠ½Ρ‹ΠΌΠΈ систСмами счислСния

ОписаниС

function base_convert(string $num, int $from_base, int $to_base): string

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Π² Π²ΠΈΠ΄Π΅ строки число num, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ функция ΠΏΠ΅Ρ€Π΅Π²Π΅Π΄Ρ‘Ρ‚ Π² систСму счислСния to_base. БистСму счислСния числа num ΡƒΠΊΠ°Π·Ρ‹Π²Π°ΡŽΡ‚ Π² ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π΅ from_base. ЗначСния from_base ΠΈ to_base Π΄ΠΎΠ»ΠΆΠ½Ρ‹ Π±Ρ‹Ρ‚ΡŒ ΠΌΠ΅ΠΆΠ΄Ρƒ 2 ΠΈ 36 Π²ΠΊΠ»ΡŽΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ. Π¦ΠΈΡ„Ρ€Ρ‹ Π² числах с основаниСм систСмы счислСния Π²Ρ‹ΡˆΠ΅ 10 функция прСдставит Π±ΡƒΠΊΠ²Π°ΠΌΠΈ a-z, Π³Π΄Π΅ Β«aΒ» ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚ 10, Β«bΒ» ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚ 11, Π° Β«zΒ» β€” 35. РСгистр Π±ΡƒΠΊΠ² Π½Π΅ Π²Π°ΠΆΠ΅Π½, Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° num интСрпрСтируСтся рСгистронСзависимо.

Π’Π½ΠΈΠΌΠ°Π½ΠΈΠ΅

Ѐункция base_convert() ΠΌΠΎΠΆΠ΅Ρ‚ ΠΏΠΎΡ‚Π΅Ρ€ΡΡ‚ΡŒ Ρ‚ΠΎΡ‡Π½ΠΎΡΡ‚ΡŒ прСдставлСния Π±ΠΎΠ»ΡŒΡˆΠΈΡ… чисСл ΠΈΠ·-Π·Π° свойств, связанных с Π²Π½ΡƒΡ‚Ρ€Π΅Π½Π½ΠΈΠΌ прСдставлСниСм чисСл с ΠΏΠ»Π°Π²Π°ΡŽΡ‰Π΅ΠΉ Ρ‚ΠΎΡ‡ΠΊΠΎΠΉ (float). ΠŸΠΎΠ΄Ρ€ΠΎΠ±Π½Π΅Π΅ ΠΎ спСцификС Π΄Π°Π½Π½Ρ‹Ρ… Ρ‚ΠΈΠΏΠΎΠ² ΠΈ ΠΈΡ… ограничСниях рассказано Π² Ρ€Π°Π·Π΄Π΅Π»Π΅ «Числа с ΠΏΠ»Π°Π²Π°ΡŽΡ‰Π΅ΠΉ Ρ‚ΠΎΡ‡ΠΊΠΎΠΉΒ».

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

num

Число для прСобразования. Π›ΡŽΠ±ΠΎΠΉ нСдопустимый символ Π² Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΈ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° num Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΡ€ΠΎΠΈΠ³Π½ΠΎΡ€ΠΈΡ€ΠΎΠ²Π°Π½ Π±Π΅Π· прСдупрСТдСния. Π‘ PHP 7.4.0 прСдоставлСниС Π»ΡŽΠ±Ρ‹Ρ… нСдопустимых символов устарСло.

from_base

ОснованиС систСмы счислСния числа num.

to_base

ОснованиС систСмы счислСния, Π² ΠΊΠΎΡ‚ΠΎΡ€ΡƒΡŽ Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΎ число num.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ число num, ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½Π½ΠΎΠ΅ Π² систСму счислСния to_base.

Бписок измСнСний

ВСрсия ОписаниС
7.4.0 ΠŸΠ΅Ρ€Π΅Π΄Π°Ρ‡Π° нСдопустимых символов Π±ΡƒΠ΄Π΅Ρ‚ Π³Π΅Π½Π΅Ρ€ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΡƒΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΠ΅ ΠΎΠ± устарСвании. Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ Π±ΡƒΠ΄Π΅Ρ‚ вычислСн Ρ‚Π°ΠΊ, ΠΊΠ°ΠΊ Ссли Π±Ρ‹ нСдопустимыС символы Π½Π΅ сущСствовали.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ base_convert()

<?php

$hexadecimal
= 'a37334';
echo
base_convert($hexadecimal, 16, 2);

?>

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°:

101000110111001100110100

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • intval() - Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ цСлочислСнноС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 6 notes

up
96
PHPCoder at niconet2k dot com ΒΆ
14 years ago
Convert an arbitrarily large number from any base to any base.

string convBase(string $numberInput, string $fromBaseInput, string $toBaseInput)
$numberInput number to convert as a string
$fromBaseInput base of the number to convert as a string
$toBaseInput base the number should be converted to as a string
examples for $fromBaseInput and $toBaseInput
'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: 173

convBase('70B1D707EAC2EDF4C6389F440C7294B51FFF57BB', '0123456789ABCDEF', '01');
//Convert '70B1D707EAC2EDF4C6389F440C7294B51FFF57BB' from hexadecimal (base16) to binary (base2).
//result: 
//111000010110001110101110000011111101010110000101110
//110111110100110001100011100010011111010001000000110
//001110010100101001011010100011111111111110101011110
//111011

convBase('1324523453243154324542341524315432113200203012', '012345', '0123456789ABCDEF');
//Convert '1324523453243154324542341524315432113200203012' from senary (base6) to hexadecimal (base16).
//result: 1F9881BAD10454A8C23A838EF00F50

convBase('355927353784509896715106760','0123456789','Christopher');
//Convert '355927353784509896715106760' from decimal (base10) to undecimal (base11) using "Christopher" as the numbers.
//result: iihtspiphoeCrCeshhorsrrtrh

convBase('1C238Ab97132aAC84B72','0123456789aAbBcCdD', '~!@#$%^&*()');
//Convert'1C238Ab97132aAC84B72' from octodecimal (base18) using '0123456789aAbBcCdD' as the numbers to undecimal (base11) using '~!@#$%^&*()' as the numbers.
//result: !%~!!*&!~^!!&(&!~^@#@@@&

function convBase($numberInput, $fromBaseInput, $toBaseInput)
{
    if ($fromBaseInput==$toBaseInput) return $numberInput;
    $fromBase = str_split($fromBaseInput,1);
    $toBase = str_split($toBaseInput,1);
    $number = str_split($numberInput,1);
    $fromLen=strlen($fromBaseInput);
    $toLen=strlen($toBaseInput);
    $numberLen=strlen($numberInput);
    $retval='';
    if ($toBaseInput == '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 ($fromBaseInput != '0123456789')
        $base10=convBase($numberInput, $fromBaseInput, '0123456789');
    else
        $base10 = $numberInput;
    if ($base10<strlen($toBaseInput))
        return $toBase[$base10];
    while($base10 != '0')
    {
        $retval = $toBase[bcmod($base10,$toLen)].$retval;
        $base10 = bcdiv($base10,$toLen,0);
    }
    return $retval;
}
?>
up
10
Anonymous ΒΆ
7 years ago
While not immediately clear from the description above, a negative sign is also "silently ignored".

base_convert("-12", 10, 10)   =>   12
up
15
lindsay at bitleap dot com ΒΆ
21 years ago
If you need to use base_convert with numbers larger then 32 bit, the following gmp implementation of base_convert should work.

<?php

/*use gmp library to convert base. gmp will convert numbers > 32bit*/
function gmp_convert($num, $base_a, $base_b)
{
        return gmp_strval ( gmp_init($num, $base_a), $base_b );
}

?>
up
5
ardavies at tiscali dot co dot uk ΒΆ
12 years ago
In order to convert base 26 (hexavigesimal) of just alphanumeric characters (A-Z), wthout integers, (as descibed at http://en.wikipedia.org/wiki/Hexavigesimal), I found this to be useful:

function base_convert_alpha(  $str,  $from,  $to  )
{
    $r = range( 'A', 'Z' );
    $clean = str_replace( $r, array_keys($r), $str );
    return base_convert( $clean, $from, $to );
}

echo base_convert_alpha(  "BAC",  26,  10  );

//$clean = 102 which then returns 678
up
2
ohcc at 163 dot com ΒΆ
8 years ago
<?php
    $v = base_convert(3.14, 10, 10);
    var_dump($v);
?>

output: string(3) "314"
up
4
cyrilbele at yahoo dot fr ΒΆ
17 years ago
If you want to do sharding, at some point you will need to decide which shard to target. 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 get the shard based on the key and the number of shards available

<?php
function getShard($key,$nbShards) {
    $num = substr(base_convert(sha1($key), 16, 10),4,6);
    return $num%$nbShards;
}
?>