十进制与十六进制互转

十六进制转十进制

十进制转十六进制

 0) {
            $d = $n % $divide;
            $n = ($n - $d) / $divide;
            $chars[] = $map[$d];
        }

        $rel = join('', array_reverse($chars));

        return $upper ? strtoupper($rel) : $rel;
    }

    /**
     * 36进制转10进制
     * @param  string 
     */
    public static function decode (String $code) {

        $char = strtolower($code);
        $map = self::$map;
        $c = count($map);
        $len = strlen($char);
        $sum = 0;

        for($i = 0; $i < $len; $i++) {
            $index = array_search($char[$i], $map);

            $sum += $index * pow($c, $len - $i - 1);
        }
        return $sum;
    }
}

示例

    $r1 = Base36::encode(10001);

    $r2 = Base36::decode($r1);

    var_dump($r1, $r2);

  // string(3) "7PT"
  //int(10001)

你可能感兴趣的:(十进制与十六进制互转)