PHP 8.x Base Conversion Guide: Binary, Octal, Decimal, and Hexadecimal

In PHP 8.x, “convert the base” is first a data-contract question. Is the input a platform-width integer or a digit string whose leading zeros matter? Will the output be used in arithmetic or only displayed? Answering those questions first prevents invalid characters, floating-point approximations, or byte encodings from being mistaken for a correct integer conversion.

The examples contain no sensitive data. Validate every string from a request, file, or database against the complete application contract before passing it to a conversion function.

Conclusions first

  • decbin(), decoct(), and dechex() accept an int and return a digit string with no prefix or padding.
  • bindec(), octdec(), and hexdec() accept strings. Their result may be an int or, beyond the native integer range, a float; reject the latter whenever an exact integer is required.
  • base_convert() returns a string for bases 2 through 36, but the official manual warns that large values can lose precision because of internal floating-point handling.
  • These parsers ignore invalid characters and have emitted deprecation notices for such input since PHP 7.4. Do not rely on that permissive behavior; validate the entire string before conversion.
  • Leading zeros, 0b / 0o / 0x prefixes, and letter case are input or display formats, not part of the numeric value. Put the policy explicitly in the interface contract.

Record the real runtime first. A 64-bit result from a development machine must not be assumed on every deployment:

<?php
declare(strict_types=1);

printf(
    "PHP %s, native integer width: %d bits%s",
    PHP_VERSION,
    PHP_INT_SIZE * 8,
    PHP_EOL
);

Function selection matrix

NeedFunctionInputOutputPrincipal boundary
Decimal integer to binary textdecbin()intstringNative int width; a negative value appears as a platform-dependent unsigned bit pattern
Decimal integer to octal textdecoct()intstringNative int width; does not add 0o
Decimal integer to hexadecimal textdechex()intstringNative int width; negatives are treated as unsigned; output is lowercase
Binary, octal, or hexadecimal text to a valuebindec() / octdec() / hexdec()stringint|floatInvalid characters are ignored; a large value may become an inexact float
Text between bases 2–36base_convert()string and two basesstringLarge values may lose precision; output letters are lowercase

Keep display format separate from numeric value. For example, "0011" and "11" both represent binary 3, but the first also carries a display width of four.

Step 1: define and validate the string contract

The validator below adopts one deliberately narrow contract: unsigned, unprefixed ASCII digits in bases 2–36. It does not trim whitespace and does not accept separators or underscores. Leading zeros remain valid. If a product needs a sign or prefix, validate the entire outer format and remove that component explicitly rather than asking a conversion function to guess.

<?php
declare(strict_types=1);

function validBaseDigits(string $digits, int $base): bool
{
    if ($digits === '' || $base < 2 || $base > 36) {
        return false;
    }

    $alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';

    foreach (str_split(strtolower($digits)) as $digit) {
        $value = strpos($alphabet, $digit);

        if ($value === false || $value >= $base) {
            return false;
        }
    }

    return true;
}

This accepts 000110 and 00FF, but rejects 0b1010, 0x2f, whitespace, a minus sign, and a digit outside the declared base. The 0b, 0o, and 0x forms are PHP source-code literal syntax; that does not mean arbitrary input strings should accept those prefixes.

Treat decimal input from HTTP as a string too. FILTER_VALIDATE_INT can validate a base-10 integer within the native range, but use a strict comparison to distinguish a valid zero from failure and decide beforehand whether whitespace, a plus sign, and leading zeros are allowed. Do not cast first: PHP’s rules for converting a “leading numeric string” are not an input validator.

$rawDecimal = '47';

if ($rawDecimal !== trim($rawDecimal)) {
    throw new InvalidArgumentException('Whitespace is not allowed');
}

$decimal = filter_var($rawDecimal, FILTER_VALIDATE_INT);

if (!is_int($decimal)) {
    throw new RangeException('Expected a native-range decimal integer');
}

Step 2: convert only within the native integer range

For binary, octal, and hexadecimal strings, validate the complete string first, then inspect the return type. If the result is a float, do not cast it back to int: precision may already have been lost.

function toNativeInt(string $digits, int $base): int
{
    if (!validBaseDigits($digits, $base)) {
        throw new InvalidArgumentException('Invalid digits for the declared base');
    }

    $value = match ($base) {
        2 => bindec($digits),
        8 => octdec($digits),
        16 => hexdec($digits),
        default => throw new InvalidArgumentException(
            'Use this native-integer helper only with base 2, 8, or 16'
        ),
    };

    if (!is_int($value)) {
        throw new RangeException('Value exceeds the native integer range');
    }

    return $value;
}

The reverse direction requires the caller to already hold a real int:

$value = 47;

$binary = decbin($value); // 101111
$octal = decoct($value);  // 57
$hex = dechex($value);    // 2f

PHP int is signed and platform-dependent. Inspect PHP_INT_SIZE, PHP_INT_MIN, and PHP_INT_MAX; PHP has no unsigned int type. decbin(-1), decoct(-1), and dechex(-1) produce a platform-width unsigned bit pattern, not -1. If the interface needs signed text, handle the sign separately and guard PHP_INT_MIN, whose absolute value cannot fit in the same signed int:

function signedHex(int $value): string
{
    if ($value === PHP_INT_MIN) {
        throw new RangeException('PHP_INT_MIN needs arbitrary-length handling');
    }

    return $value < 0
        ? '-' . dechex(-$value)
        : dechex($value);
}

Do not pass floats to these integer functions, and do not convert an out-of-range string to float and back. Converting an out-of-range float to int can have undefined results.

Step 3: separate display formatting from value

decbin(), decoct(), dechex(), and base_convert() neither preserve leading zeros nor add base prefixes. dechex() and base_convert() emit lowercase letters. Add a fixed width, uppercase, or a prefix only after successful conversion:

$numericValue = 47;
$digits = strtoupper(str_pad(dechex($numericValue), 4, '0', STR_PAD_LEFT));
$display = '0x' . $digits;

echo $display; // 0x002F

$numericValue remains 47; $digits and $display are presentation strings. A fixed width also implies a bound: if the result is longer than the requested width, fail or expand it; never truncate the left side.

Step 4: use base_convert() cautiously

base_convert($num, $fromBase, $toBase) supports bases 2–36, reads letters case-insensitively, and emits lowercase letters. It is suitable for validated, bounded identifiers or display conversions, not for numbers that require arbitrary-length exactness.

$source = '00ff';

if (!validBaseDigits($source, 16)) {
    throw new InvalidArgumentException('Invalid hexadecimal digits');
}

$binary = base_convert($source, 16, 2);

echo $binary; // 11111111

The leading zeros disappear because the result represents a numeric value. If the four-character width of 00ff has business meaning, retain the width or the original normalized display value separately.

Do not confuse bin2hex() with base conversion

bin2hex() encodes the bytes of a string as hexadecimal. It does not interpret the characters "10" as the binary number 2:

echo bin2hex('10');            // 3130: ASCII bytes for "10"
echo base_convert('10', 2, 16); // 2: binary digits to hexadecimal

For a file, protocol frame, or cryptographic byte sequence, bin2hex() may be exactly right. For numeral text made of 0 and 1, validate it and perform a base conversion.

Beyond native width: use an explicit arbitrary-length route

When a value may exceed PHP_INT_MAX, do not treat a float from bindec(), octdec(), or hexdec() as exact, and do not rely on base_convert(). PHP’s GMP extension provides arbitrary-length integers: gmp_init() parses a string with an explicit base, and gmp_strval() emits a string in the target base.

if (!extension_loaded('gmp')) {
    throw new RuntimeException('GMP extension is required');
}

$large = gmp_init('ffffffffffffffff', 16);
$decimal = gmp_strval($large, 10);

echo $decimal; // 18446744073709551615

Still validate the string against its declared base first. Passing an explicit base avoids ambiguity from automatic prefix detection. GMP is not installed on this workstation, so this block was checked statically against the official PHP API and documented boundaries rather than executed locally.

BCMath is an arbitrary-precision decimal-string mathematics extension, not a general replacement for base_convert() across bases 2–36. Choose BCMath only when the business values are already validated decimal strings and decimal arithmetic is required; do not route them through float.

Safe web output

A validated conversion result is not the same as HTML context encoding. Controlled base digits contain only ASCII characters, but consistently call htmlspecialchars() at the template boundary, especially when the result is combined with other dynamic content:

header('Content-Type: text/html; charset=UTF-8');

$rendered = strtoupper(str_pad(dechex(47), 4, '0', STR_PAD_LEFT));

echo '<output>'
    . htmlspecialchars($rendered, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8')
    . '</output>';

Do not reflect unvalidated raw input on an error page or write it to public logs. URLs, JavaScript, CSS, and HTTP headers require their own contextual handling; htmlspecialchars() alone does not cover them.

Reproducible PHP 8.x CLI assertions

This self-contained test does not depend on assert(), which may be disabled. Save it and run it with a supported PHP 8.x CLI; any mismatch throws and exits nonzero.

<?php
declare(strict_types=1);

function expectSame(mixed $expected, mixed $actual): void
{
    if ($expected !== $actual) {
        throw new RuntimeException('Conversion assertion failed');
    }
}

function validBaseDigits(string $digits, int $base): bool
{
    if ($digits === '' || $base < 2 || $base > 36) {
        return false;
    }

    $alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';

    foreach (str_split(strtolower($digits)) as $digit) {
        $value = strpos($alphabet, $digit);

        if ($value === false || $value >= $base) {
            return false;
        }
    }

    return true;
}

function toNativeInt(string $digits, int $base): int
{
    if (!validBaseDigits($digits, $base)) {
        throw new InvalidArgumentException('Invalid digits for the declared base');
    }

    $value = match ($base) {
        2 => bindec($digits),
        8 => octdec($digits),
        16 => hexdec($digits),
        default => throw new InvalidArgumentException(
            'Use this native-integer helper only with base 2, 8, or 16'
        ),
    };

    if (!is_int($value)) {
        throw new RangeException('Value exceeds the native integer range');
    }

    return $value;
}

function signedHex(int $value): string
{
    if ($value === PHP_INT_MIN) {
        throw new RangeException('PHP_INT_MIN needs arbitrary-length handling');
    }

    return $value < 0
        ? '-' . dechex(-$value)
        : dechex($value);
}

expectSame('1100', decbin(12));
expectSame('410', decoct(264));
expectSame('2f', dechex(47));
expectSame(6, toNativeInt('000110', 2));
expectSame(15, toNativeInt('17', 8));
expectSame(47, toNativeInt('2F', 16));
expectSame(false, validBaseDigits('0b1010', 2));
expectSame(false, validBaseDigits('12oops', 10));
expectSame('11111111', base_convert('00ff', 16, 2));
expectSame('002F', strtoupper(str_pad(dechex(47), 4, '0', STR_PAD_LEFT)));
expectSame('-2f', signedHex(-47));
expectSame(str_repeat('1', PHP_INT_SIZE * 8), decbin(-1));
expectSame('3130', bin2hex('10'));
expectSame('2', base_convert('10', 2, 16));

echo "PHP ", PHP_VERSION, "; ", PHP_INT_SIZE * 8, "-bit; tests passed", PHP_EOL;

These tests cover the native-integer path. They do not prove that out-of-range input remains exact through float or base_convert(). Production code should add boundary cases for the actual minimum, maximum, length, and output width it permits.

Pre-release checklist

  • Identify whether the input is a native integer, a digit string, or an arbitrary-length integer.
  • Fix the allowed base, sign, prefix, case, length, and leading-zero policy for every field.
  • Validate the entire string before conversion; never rely on invalid characters being ignored.
  • Check is_int() on results from bindec(), octdec(), and hexdec().
  • Do not route out-of-range values through float or use base_convert() where arbitrary-length exactness is required.
  • Store the value, digit string, and display prefix/padding separately.
  • For negatives, choose explicitly between signed text and a fixed-width bit pattern, and record the width.
  • Run boundary tests on the target PHP version and 32/64-bit deployment environment.
  • Encode at the HTML template boundary; do not log untreated raw input.

Official references

Historical source archive (provenance only)

The complete visible body from source_export follows verbatim. Nothing was removed, rewritten, whitespace-normalized, or redacted for privacy or safety. It reflects PHP versions and explanations from 2011; its width limits, permissive parsing, and function usage are not current recommendations. The entire block is inert plain text and should not be executed or copied into production.

PHP函数篇详解十进制、二进制、八进制和十六进制互相转换函数说明

主要掌握各进制转换的方法,以应用于实际开发。

Table of Contents

Toggle

- [十进制(decimal system)转换函数说明](https://blog.lazying.art/en/html/computer_internet/php/736/php%e5%87%bd%e6%95%b0%e7%af%87%e8%af%a6%e8%a7%a3%e5%8d%81%e8%bf%9b%e5%88%b6%e3%80%81%e4%ba%8c%e8%bf%9b%e5%88%b6%e3%80%81%e5%85%ab%e8%bf%9b%e5%88%b6%e5%92%8c%e5%8d%81%e5%85%ad%e8%bf%9b%e5%88%b6.html/#%E5%8D%81%E8%BF%9B%E5%88%B6%EF%BC%88decimal_system%EF%BC%89%E8%BD%AC%E6%8D%A2%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E)

  - [十进制转二进制 decbin() 函数](https://blog.lazying.art/en/html/computer_internet/php/736/php%e5%87%bd%e6%95%b0%e7%af%87%e8%af%a6%e8%a7%a3%e5%8d%81%e8%bf%9b%e5%88%b6%e3%80%81%e4%ba%8c%e8%bf%9b%e5%88%b6%e3%80%81%e5%85%ab%e8%bf%9b%e5%88%b6%e5%92%8c%e5%8d%81%e5%85%ad%e8%bf%9b%e5%88%b6.html/#%E5%8D%81%E8%BF%9B%E5%88%B6%E8%BD%AC%E4%BA%8C%E8%BF%9B%E5%88%B6_decbin_%E5%87%BD%E6%95%B0)
  - [十进制转八进制 decoct() 函数](https://blog.lazying.art/en/html/computer_internet/php/736/php%e5%87%bd%e6%95%b0%e7%af%87%e8%af%a6%e8%a7%a3%e5%8d%81%e8%bf%9b%e5%88%b6%e3%80%81%e4%ba%8c%e8%bf%9b%e5%88%b6%e3%80%81%e5%85%ab%e8%bf%9b%e5%88%b6%e5%92%8c%e5%8d%81%e5%85%ad%e8%bf%9b%e5%88%b6.html/#%E5%8D%81%E8%BF%9B%E5%88%B6%E8%BD%AC%E5%85%AB%E8%BF%9B%E5%88%B6_decoct_%E5%87%BD%E6%95%B0)
  - [十进制转十六进制 dechex() 函数](https://blog.lazying.art/en/html/computer_internet/php/736/php%e5%87%bd%e6%95%b0%e7%af%87%e8%af%a6%e8%a7%a3%e5%8d%81%e8%bf%9b%e5%88%b6%e3%80%81%e4%ba%8c%e8%bf%9b%e5%88%b6%e3%80%81%e5%85%ab%e8%bf%9b%e5%88%b6%e5%92%8c%e5%8d%81%e5%85%ad%e8%bf%9b%e5%88%b6.html/#%E5%8D%81%E8%BF%9B%E5%88%B6%E8%BD%AC%E5%8D%81%E5%85%AD%E8%BF%9B%E5%88%B6_dechex_%E5%87%BD%E6%95%B0)

- [二进制(binary system)转换函数说明](https://blog.lazying.art/en/html/computer_internet/php/736/php%e5%87%bd%e6%95%b0%e7%af%87%e8%af%a6%e8%a7%a3%e5%8d%81%e8%bf%9b%e5%88%b6%e3%80%81%e4%ba%8c%e8%bf%9b%e5%88%b6%e3%80%81%e5%85%ab%e8%bf%9b%e5%88%b6%e5%92%8c%e5%8d%81%e5%85%ad%e8%bf%9b%e5%88%b6.html/#%E4%BA%8C%E8%BF%9B%E5%88%B6%EF%BC%88binary_system%EF%BC%89%E8%BD%AC%E6%8D%A2%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E)

  - [二进制转十制进 bindec() 函数](https://blog.lazying.art/en/html/computer_internet/php/736/php%e5%87%bd%e6%95%b0%e7%af%87%e8%af%a6%e8%a7%a3%e5%8d%81%e8%bf%9b%e5%88%b6%e3%80%81%e4%ba%8c%e8%bf%9b%e5%88%b6%e3%80%81%e5%85%ab%e8%bf%9b%e5%88%b6%e5%92%8c%e5%8d%81%e5%85%ad%e8%bf%9b%e5%88%b6.html/#%E4%BA%8C%E8%BF%9B%E5%88%B6%E8%BD%AC%E5%8D%81%E5%88%B6%E8%BF%9B_bindec_%E5%87%BD%E6%95%B0)
  - [二进制转十六制进 bin2hex() 函数](https://blog.lazying.art/en/html/computer_internet/php/736/php%e5%87%bd%e6%95%b0%e7%af%87%e8%af%a6%e8%a7%a3%e5%8d%81%e8%bf%9b%e5%88%b6%e3%80%81%e4%ba%8c%e8%bf%9b%e5%88%b6%e3%80%81%e5%85%ab%e8%bf%9b%e5%88%b6%e5%92%8c%e5%8d%81%e5%85%ad%e8%bf%9b%e5%88%b6.html/#%E4%BA%8C%E8%BF%9B%E5%88%B6%E8%BD%AC%E5%8D%81%E5%85%AD%E5%88%B6%E8%BF%9B_bin2hex_%E5%87%BD%E6%95%B0)

- [八进制(octal system)转换函数说明](https://blog.lazying.art/en/html/computer_internet/php/736/php%e5%87%bd%e6%95%b0%e7%af%87%e8%af%a6%e8%a7%a3%e5%8d%81%e8%bf%9b%e5%88%b6%e3%80%81%e4%ba%8c%e8%bf%9b%e5%88%b6%e3%80%81%e5%85%ab%e8%bf%9b%e5%88%b6%e5%92%8c%e5%8d%81%e5%85%ad%e8%bf%9b%e5%88%b6.html/#%E5%85%AB%E8%BF%9B%E5%88%B6%EF%BC%88octal_system%EF%BC%89%E8%BD%AC%E6%8D%A2%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E)

  - [八进制转十进制 octdec() 函数](https://blog.lazying.art/en/html/computer_internet/php/736/php%e5%87%bd%e6%95%b0%e7%af%87%e8%af%a6%e8%a7%a3%e5%8d%81%e8%bf%9b%e5%88%b6%e3%80%81%e4%ba%8c%e8%bf%9b%e5%88%b6%e3%80%81%e5%85%ab%e8%bf%9b%e5%88%b6%e5%92%8c%e5%8d%81%e5%85%ad%e8%bf%9b%e5%88%b6.html/#%E5%85%AB%E8%BF%9B%E5%88%B6%E8%BD%AC%E5%8D%81%E8%BF%9B%E5%88%B6_octdec_%E5%87%BD%E6%95%B0)

- [十六进制(hexadecimal)转换函数说明](https://blog.lazying.art/en/html/computer_internet/php/736/php%e5%87%bd%e6%95%b0%e7%af%87%e8%af%a6%e8%a7%a3%e5%8d%81%e8%bf%9b%e5%88%b6%e3%80%81%e4%ba%8c%e8%bf%9b%e5%88%b6%e3%80%81%e5%85%ab%e8%bf%9b%e5%88%b6%e5%92%8c%e5%8d%81%e5%85%ad%e8%bf%9b%e5%88%b6.html/#%E5%8D%81%E5%85%AD%E8%BF%9B%E5%88%B6%EF%BC%88hexadecimal%EF%BC%89%E8%BD%AC%E6%8D%A2%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E)

  - [十六进制转十进制 hexdec()函数](https://blog.lazying.art/en/html/computer_internet/php/736/php%e5%87%bd%e6%95%b0%e7%af%87%e8%af%a6%e8%a7%a3%e5%8d%81%e8%bf%9b%e5%88%b6%e3%80%81%e4%ba%8c%e8%bf%9b%e5%88%b6%e3%80%81%e5%85%ab%e8%bf%9b%e5%88%b6%e5%92%8c%e5%8d%81%e5%85%ad%e8%bf%9b%e5%88%b6.html/#%E5%8D%81%E5%85%AD%E8%BF%9B%E5%88%B6%E8%BD%AC%E5%8D%81%E8%BF%9B%E5%88%B6_hexdec%E5%87%BD%E6%95%B0)

- [任意进制转换 base_convert() 函数](https://blog.lazying.art/en/html/computer_internet/php/736/php%e5%87%bd%e6%95%b0%e7%af%87%e8%af%a6%e8%a7%a3%e5%8d%81%e8%bf%9b%e5%88%b6%e3%80%81%e4%ba%8c%e8%bf%9b%e5%88%b6%e3%80%81%e5%85%ab%e8%bf%9b%e5%88%b6%e5%92%8c%e5%8d%81%e5%85%ad%e8%bf%9b%e5%88%b6.html/#%E4%BB%BB%E6%84%8F%E8%BF%9B%E5%88%B6%E8%BD%AC%E6%8D%A2_base_convert_%E5%87%BD%E6%95%B0)

## 十进制(decimal system)转换函数说明

### 十进制转二进制 decbin() 函数

如下实例

1. echo decbin(12); //输出 1100
2. echo decbin(26); //输出 11010

**decbin**
 (PHP 3, PHP 4, PHP 5)
 decbin — 十进制转换为二进制
 说明
 string decbin ( int number )
 返回一字符串,包含有给定 number 参数的二进制表示。所能转换的最大数值为十进制的 4294967295,其结果为 32 个 1 的字符串。

### 十进制转八进制 decoct() 函数

1. echo decoct(15); //输出 17
2. echo decoct(264); //输出 410

**decoct**
 (PHP 3, PHP 4, PHP 5)
 decoct — 十进制转换为八进制
 说明
 string decoct ( int number )
 返回一字符串,包含有给定 number 参数的八进制表示。所能转换的最大数值为十进制的 4294967295,其结果为 “37777777777”。

### 十进制转十六进制 dechex() 函数

1. echo dechex(10); //输出 a
2. echo dechex(47); //输出 2f

**dechex**
 (PHP 3, PHP 4, PHP 5)
 dechex — 十进制转换为十六进制
 说明
 string dechex ( int number )
 返回一字符串,包含有给定 number 参数的十六进制表示。所能转换的最大数值为十进制的 4294967295,其结果为 “ffffffff”。

## 二进制(binary system)转换函数说明

### 二进制转十制进 bindec() 函数

1. echo bindec(‘110011’); //输出 51
2. echo bindec(‘000110011’); //输出 51
3. echo bindec(‘111’); //输出 7

**bindec**
 (PHP 3, PHP 4, PHP 5)
 bindec — 二进制转换为十进制
 说明
 number bindec ( string binary_string )
 返回 binary_string 参数所表示的二进制数的十进制等价值。
 bindec() 将一个二进制数转换成 integer。可转换的最大的数为 31 位 1 或者说十进制的 2147483647。PHP 4.1.0 开始,该函数可以处理大数值,这种情况下,它会返回 float 类型。

### 二进制转十六制进 bin2hex() 函数

1. $binary = “11111001”;
2. $hex = dechex(bindec($binary));
3. echo $hex;//输出f9

**bin2hex**
 (PHP 3 >= 3.0.9, PHP 4, PHP 5)
 bin2hex — 将二进制数据转换成十六进制表示
 说明
 string bin2hex ( string str )
 返回 ASCII 字符串,为参数 str 的十六进制表示。转换使用字节方式,高四位字节优先。

## 八进制(octal system)转换函数说明

### 八进制转十进制 octdec() 函数

1. echo octdec(’77’); //输出 63
2. echo octdec(decoct(45)); //输出 45

**octdec**
 (PHP 3, PHP 4, PHP 5)
 octdec — 八进制转换为十进制
 说明
 number octdec ( string octal_string )
 返回 octal_string 参数所表示的八进制数的十进制等值。可转换的最大的数值为 17777777777 或十进制的 2147483647。PHP 4.1.0 开始,该函数可以处理大数字,这种情况下,它会返回 float 类型。

## 十六进制(hexadecimal)转换函数说明

### 十六进制转十进制 hexdec()函数

1. var_dump(hexdec(“See”));
2. var_dump(hexdec(“ee”));
3. // both print “int(238)”
4. var_dump(hexdec(“that”)); // print “int(10)”
5. var_dump(hexdec(“a0”)); // print “int(160)”

**hexdec**
 (PHP 3, PHP 4, PHP 5)
 hexdec — 十六进制转换为十进制
 说明
 number hexdec ( string hex_string )
 返回与 hex_string 参数所表示的十六进制数等值的的十进制数。hexdec() 将一个十六进制字符串转换为十进制数。所能转换的最大数值为 7fffffff,即十进制的 2147483647。PHP 4.1.0 开始,该函数可以处理大数字,这种情况下,它会返回 float 类型。
 hexdec() 将遇到的所有非十六进制字符替换成 0。这样,所有左边的零都被忽略,但右边的零会计入值中。

## 任意进制转换 base_convert() 函数

1. $hexadecimal = ‘A37334’;
2. echo base_convert($hexadecimal, 16, 2);//输出 101000110111001100110100

**base_convert**
 (PHP 3 >= 3.0.6, PHP 4, PHP 5)

base_convert — 在任意进制之间转换数字
 说明
 string base_convert ( string number, int frombase, int tobase )
 返回一字符串,包含 number 以 tobase 进制的表示。number 本身的进制由 frombase 指定。frombase 和 tobase 都只能在 2 和 36 之间(包括 2 和 36)。高于十进制的数字用字母 a-z 表示,例如 a 表示 10,b 表示 11 以及 z 表示 35。

Leave a Reply