Bitpay\Util\Util::decodeHex PHP Метод

decodeHex() публичный статический Метод

Decodes a hexadecimal value into decimal.
public static decodeHex ( string $hex ) : string
$hex string
Результат string
    public static function decodeHex($hex)
    {
        if (!is_string($hex) || !ctype_xdigit($hex) && '0x' != substr($hex, 0, 2)) {
            throw new \Exception('Argument must be a string of hex digits.');
        }
        $hex = strtolower($hex);
        // if it has a prefix of 0x this needs to be trimed
        if (substr($hex, 0, 2) == '0x') {
            $hex = substr($hex, 2);
        }
        $hexLen = strlen($hex);
        for ($dec = '0', $i = 0; $i < $hexLen; $i++) {
            $current = strpos(self::HEX_CHARS, $hex[$i]);
            $dec = Math::add(Math::mul($dec, 16), $current);
        }
        return $dec;
    }

Usage Example

Пример #1
0
 /**
  * Encodes $data into BASE-58 format
  *
  * @param string $data
  *
  * @return string
  */
 public static function encode($data)
 {
     if (strlen($data) % 2 != 0 || strlen($data) == 0) {
         throw new \Exception('Invalid Length');
     }
     $code_string = self::BASE58_CHARS;
     $x = Util::decodeHex($data);
     $output_string = '';
     while (Math::cmp($x, '0') > 0) {
         $q = Math::div($x, 58);
         $r = Math::mod($x, 58);
         $output_string .= substr($code_string, intval($r), 1);
         $x = $q;
     }
     for ($i = 0; $i < strlen($data) && substr($data, $i, 2) == '00'; $i += 2) {
         $output_string .= substr($code_string, 0, 1);
     }
     $output_string = strrev($output_string);
     return $output_string;
 }
All Usage Examples Of Bitpay\Util\Util::decodeHex