Advertisement
femalefaust

convBase.php

Sep 20th, 2021
2,079
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.86 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Convert an arbitrarily large number from any base to any base.
  5.  
  6. string convBase(string $numberInput, string $fromBaseInput, string $toBaseInput)
  7.  $numberInput number to convert as a string
  8.  $fromBaseInput base of the number to convert as a string
  9.  $toBaseInput base the number should be converted to as a string
  10.  examples for $fromBaseInput and $toBaseInput
  11.  '0123456789ABCDEF' for Hexadecimal (Base16)
  12.  '0123456789' for Decimal (Base10)
  13.  '01234567' for Octal (Base8)
  14.  '01' for Binary (Base2)
  15. You can really put in whatever you want and the first character is the 0.
  16.  Examples:
  17. */
  18.  
  19. convBase('123', '0123456789', '01234567');
  20. //Convert '123' from decimal (base10) to octal (base8).
  21.  //result: 173
  22.  
  23. convBase('70B1D707EAC2EDF4C6389F440C7294B51FFF57BB', '0123456789ABCDEF', '01');
  24.  //Convert '70B1D707EAC2EDF4C6389F440C7294B51FFF57BB' from hexadecimal (base16) to binary (base2).
  25.  //result:
  26. //111000010110001110101110000011111101010110000101110
  27.  //110111110100110001100011100010011111010001000000110
  28.  //001110010100101001011010100011111111111110101011110
  29.  //111011
  30.  
  31. convBase('1324523453243154324542341524315432113200203012', '012345', '0123456789ABCDEF');
  32.  //Convert '1324523453243154324542341524315432113200203012' from senary (base6) to hexadecimal (base16).
  33.  //result: 1F9881BAD10454A8C23A838EF00F50
  34.  
  35. convBase('355927353784509896715106760','0123456789','Christopher');
  36.  //Convert '355927353784509896715106760' from decimal (base10) to undecimal (base11) using "Christopher" as the numbers.
  37.  //result: iihtspiphoeCrCeshhorsrrtrh
  38.  
  39. convBase('1C238Ab97132aAC84B72','0123456789aAbBcCdD', '~!@#$%^&*()');
  40.  //Convert'1C238Ab97132aAC84B72' from octodecimal (base18) using '0123456789aAbBcCdD' as the numbers to undecimal (base11) using '~!@#$%^&*()' as the numbers.
  41.  //result: !%~!!*&!~^!!&(&!~^@#@@@&
  42.  
  43. function convBase($numberInput, $fromBaseInput, $toBaseInput)
  44.  {
  45.      if ($fromBaseInput==$toBaseInput) return $numberInput;
  46.      $fromBase = str_split($fromBaseInput,1);
  47.      $toBase = str_split($toBaseInput,1);
  48.      $number = str_split($numberInput,1);
  49.      $fromLen=strlen($fromBaseInput);
  50.      $toLen=strlen($toBaseInput);
  51.      $numberLen=strlen($numberInput);
  52.      $retval='';
  53.      if ($toBaseInput == '0123456789')
  54.      {
  55.          $retval=0;
  56.          for ($i = 1;$i <= $numberLen; $i++)
  57.              $retval = bcadd($retval, bcmul(array_search($number[$i-1], $fromBase),bcpow($fromLen,$numberLen-$i)));
  58.          return $retval;
  59.      }
  60.      if ($fromBaseInput != '0123456789')
  61.          $base10=convBase($numberInput, $fromBaseInput, '0123456789');
  62.      else
  63.          $base10 = $numberInput;
  64.      if ($base10<strlen($toBaseInput))
  65.          return $toBase[$base10];
  66.      while($base10 != '0')
  67.      {
  68.          $retval = $toBase[bcmod($base10,$toLen)].$retval;
  69.          $base10 = bcdiv($base10,$toLen,0);
  70.      }
  71.      return $retval;
  72.  }
  73. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement