1.接口文档 


 2.laravel实现代码

 /**
     * 进制转换计算器
     * @return \Illuminate\Http\JsonResponse
     */
    public function binaryConvertCal()
    {
        $ten = $this->request('ten');
        $two = $this->request('two');
        $four = $this->request('four');
        $eight = $this->request('eight');
        $sixteen = $this->request('sixteen');
        $thirty_two = $this->request('thirty_two');
        $type = intval($this->request('type')) ?: 10;        $data = [
            'ten' => $ten,
            'two' => $two,
            'four' => $four,
            'eight' => $eight,
            'sixteen' => $sixteen,
            'thirty_two' => $thirty_two
        ];        if ($type == 10 && $ten) {
            // 1.十进制
            $data['ten'] = $ten;            // 2.将十进制转换二进制
            $data['two'] = decbin($ten);            // 3.十进制转换四进制
            $data['four'] = $this->decimalToBase($ten, 4);            // 4.十进制转换八进制
            $data['eight'] = decoct($ten);            // 5.十进制转换十六进制
            $data['sixteen'] = dechex($ten);;            // 6.十进制转换三十二进制
            $data['thirty_two'] = $this->base10ToBase32($ten);
        }        if ($type == 2 && $two) {
            // 1.二进制转换十进制
            $ten = bindec($two);
            $data['ten'] = $ten ?: 'NAN';            // 2.二进制
            $data['two'] = $two;            // 3.二进制转换为四进制
            $fourNumber = bindec($two); // 转换为10进制
            $fourNumber = base_convert($fourNumber, 10, 4); // 转换为4进制
            $data['four'] = $fourNumber ?: 'NAN';            // 4.二进制转换为八进制
            $eightNumber = bindec($two); // 转换为10进制
            $eight = base_convert($eightNumber, 10, 8); // 转换为8进制
            $data['eight'] = $eight ?: 'NAN';            // 5.二进制转换为十六进制
            $sixteenNumber = bindec($two); // 转换为10进制
            $sixteen = base_convert($sixteenNumber, 10, 16); // 转换为16进制
            $data['sixteen'] = $sixteen ?: 'NAN';            // 6.二进制转换为三十二进制
            $decimalNumber = bindec($two); // 将二进制数字转换为十进制
            $thirty_two = base_convert($decimalNumber, 10, 32); // 将十进制数字转换为32进制
            $data['thirty_two'] = $thirty_two ?: 'NAN';
        }        if ($type == 4 && $four) {
            // 1.四进制转换十进制
            $ten = $this->base4_to_decimal($four);
            $data['ten'] = $ten ?: 'NAN';            // 2.四进制转换为二进制
            $two = $this->base4_to_binary($four);
            $data['two'] = $two ?: 'NAN';            // 3.四进制
            $data['four'] = $four;            // 4.四进制转换为八进制
            $eight = $this->base4_to_octal($four);
            $data['eight'] = $eight ?: 'NAN';            // 5.四进制转换为十六进制
            $sixteen = $this->base4_to_hexadecimal($four);
            $data['sixteen'] = $sixteen ?: 'NAN';            // 6.四进制转换为三十二进制
            $thirty_two = $this->base4_to_base32($four);
            $data['thirty_two'] = $thirty_two ?: 'NAN';
        }        if ($type == 8 && $eight) {
            // 1. 8进制转换为10进制
            $ten = octdec($eight);
            $data['ten'] = $ten ?: 'NAN';            // 2. 8制转换为二进制
            $two = base_convert($eight, 8, 2);
            $data['two'] = $two ?: 'NAN';            // 3. 8进制转换为4进制
            $four = base_convert($eight, 8, 4);
            $data['four'] = $four ?: 'NAN';            // 4. 8进制
            $data['eight'] = $eight;            // 5. 8进制转换为16进制
            $sixteen = base_convert($eight, 8, 16);
            $data['sixteen'] = $sixteen ?: 'NAN';            // 6. 8进制转换为32进制
            $base32Chars = '0123456789ABCDEFGHIJKLMNOPQRSTUV';
            $base32Number = '';
            $decimalNumber = octdec($eight);
            while ($decimalNumber > 0) {
                $remainder = $decimalNumber % 32;
                $base32Number = $base32Chars[$remainder] . $base32Number;
                $decimalNumber = intdiv($decimalNumber, 32);
            }
            $data['thirty_two'] = $base32Number ?: 'NAN';
        }        if ($type == 16 && $sixteen) {
            // 1. 将16进制转换为10进制
            $ten = hexdec($sixteen);
            $data['ten'] = $ten ?: 'NAN';            // 2. 16制转换为2进制
            $two = base_convert($sixteen, 16, 2);
            $data['two'] = $two ?: 'NAN';            // 3. 16进制转换为4进制
            $four = base_convert($sixteen, 16, 4);
            $data['four'] = $four ?: 'NAN';            // 4. 16进制转换为8进制
            $eight = base_convert($sixteen, 16, 8);
            $data['eight'] = $eight;            // 5. 16进制
            $data['sixteen'] = $sixteen;            // 6. 将16进制转换为32进制
            $thirty_two = $this->hexToBase32($sixteen);
            $data['thirty_two'] = $thirty_two ?: 'NAN';
        }        if ($type == 32 && $thirty_two) {
            // 1. 将32进制转换为10进制
            $ten = $this->base32ToDecimal($thirty_two);
            $data['ten'] = $ten ?: 'NAN';            // 2. 32制转换为2进制
            $two = $this->base32ToBinary($thirty_two);
            $data['two'] = $two ?: 'NAN';            // 3. 32进制转换为4进制
            $four = $this->base32ToQuaternary($thirty_two);
            $data['four'] = $four ?: 'NAN';            // 4. 32进制转换为8进制
            $eight = $this->base32ToOctal($thirty_two);
            $data['eight'] = $eight;            // 5. 32进制转换为16进制
            $data['sixteen'] = $this->base32ToHex($thirty_two);            // 6. 32进制
            $data['thirty_two'] = $thirty_two;
        }        return $this->jsonSuc(['result' => $data]);
    }    /**
     * 十进制转换
     * @param $decimal
     * @param $base
     * @return string
     */
    function decimalToBase($decimal, $base)
    {
        $result = '';
        while ($decimal > 0) {
            $remainder = $decimal % $base;
            $result = $remainder . $result;
            $decimal = (int)($decimal / $base);
        }
        return $result;
    }    /**
     * 十进制转换为三十二
     * @param $num
     * @return string
     */
    function base10ToBase32($num)
    {
        $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUV';
        $base32 = '';        while ($num > 0) {
            $remainder = $num % 32;
            $base32 = $chars[$remainder] . $base32;
            $num = (int)($num / 32);
        }        return ($base32 === '') ? '0' : $base32;
    }    /**
     * 4转换为10进制
     * @param $num
     * @return float|int
     */
    function base4_to_decimal($num)
    {
        $decimal = 0;
        $base = 4;
        $num_length = strlen($num);        for ($i = 0; $i < $num_length; $i++) {
            $digit = intval($num[$i]);
            $decimal += $digit * pow($base, $num_length - $i - 1);
        }        return $decimal;
    }    /**
     * 4转换为2进制
     * @param $num
     * @return string
     */
    function base4_to_binary($num)
    {
        $decimal = $this->base4_to_decimal($num);
        return decbin($decimal);
    }    /**
     * 4转换为8进制
     * @param $num
     * @return string
     */
    function base4_to_octal($num)
    {
        $decimal = $this->base4_to_decimal($num);
        return decoct($decimal);
    }    /**
     * 4转换为16进制
     * @param $num
     * @return string
     */
    function base4_to_hexadecimal($num)
    {
        $decimal = $this->base4_to_decimal($num);
        return dechex($decimal);
    }    /**
     * 4转换为32进制
     * @param $num
     * @return string
     */
    function base4_to_base32($num)
    {
        $decimal = $this->base4_to_decimal($num);
        return base_convert($decimal, 10, 32);
    }    /**
     * 16转32进制
     * @param $hex
     * @return string
     */
    function hexToBase32($hex)
    {
        $base32Chars = '0123456789ABCDEFGHIJKLMNOPQRSTUV';
        $result = '';        // 将十六进制字符串拆分成两个字符一组
        $hexPairs = str_split($hex, 2);        // 将每个十六进制对转换为十进制,并将其转换为五位长的三十二进制数
        foreach ($hexPairs as $pair) {
            $dec = hexdec($pair);
            $base32 = '';            while ($dec >= 32) {
                $rem = $dec % 32;
                $base32 = $base32Chars[$rem] . $base32;
                $dec = (int)($dec / 32);
            }            $base32 = $base32Chars[$dec] . $base32;
            $result .= $base32;
        }        return $result;
    }    /**
     * 将32进制转换为10进制
     * @param string $num 32进制字符串
     * @return int 转换后的10进制数值
     */
    function base32ToDecimal($num)
    {
        $base32_chars = "0123456789abcdefghijklmnopqrstuv";
        $dec = 0;
        $len = strlen($num);        for ($i = 0; $i < $len; $i++) {
            $pos = strpos($base32_chars, $num[$i]);
            $dec = 32 * $dec + $pos;
        }        return $dec;
    }    /**
     * 将32进制转换为2进制
     * @param string $num 32进制字符串
     * @return string 转换后的2进制字符串
     */
    function base32ToBinary($num)
    {
        $dec = $this->base32ToDecimal($num);
        $binary = decbin($dec);        return $binary;
    }    /**
     * 将32进制转换为4进制
     * @param string $num 32进制字符串
     * @return string 转换后的4进制字符串
     */
    function base32ToQuaternary($num)
    {
        $dec = $this->base32ToDecimal($num);
        $quaternary = base_convert($dec, 10, 4);        return $quaternary;
    }    /**
     * 将32进制转换为8进制
     * @param string $num 32进制字符串
     * @return string 转换后的8进制字符串
     */
    function base32ToOctal($num)
    {
        $dec = $this->base32ToDecimal($num);
        $octal = decoct($dec);        return $octal;
    }    /**
     * 将32进制转换为16进制
     * @param string $num 32进制字符串
     * @return string 转换后的16进制字符串
     */
    function base32ToHex($num)
    {
        $dec = $this->base32ToDecimal($num);
        $hex = dechex($dec);        return $hex;
    }

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部