PHP String to Hexadecimal

PHP developers frequently encounter challenges when working with multi-encoding systems (e.g., UTF-8, GBK, BIG5), especially with non-Latin characters. This guide explores robust methods for string-to-hex conversion while addressing encoding complexities and performance considerations.

1. Using bin2hex Function

PHP's native bin2hex provides a binary-safe solution for ASCII and ISO-8859-1 strings:

<?php
$text = "Hello Developer";
$hex = bin2hex($text);
echo $hex; // Outputs: 48656c6c6f20446576656c6f706572
?>

Outputs lowercase hex by default (use strtoupper(bin2hex(...)) for uppercase).
Fails to handle multi-byte encodings like UTF-8 natively (see Section 3).

2. Manual Conversion with unpack

For custom formatting requirements:

<?php
function stringToHex($input) {
    $chars = unpack('H*', $input);
    return current($chars);
}
echo stringToHex("PHP Hex"); // Outputs: 50485020486578
?>

Flexible output formatting (e.g., uppercase, separators).

Compatible with binary data (images, encrypted payloads).

3. Handling Multibyte Characters

For UTF-8 string conversion:

<?php
function mbStringToHex($input, $encoding = 'UTF-8') {
    $hex = [];
    // Convert to target encoding before processing
    $converted = mb_convert_encoding($input, 'UTF-8', $encoding);
    $length = mb_strlen($converted, 'UTF-8');
    for ($i = 0; $i < $length; $i++) {
        $char = mb_substr($converted, $i, 1, 'UTF-8');
        $hex[] = sprintf("%02x", hexdec(bin2hex($char)));
    }
    return implode('', $hex);
}

// Example: Convert GBK Chinese text
$gbkText = iconv('UTF-8', 'GBK', '中文测试');
echo mbStringToHex($gbkText, 'GBK'); // Output: d6d0cec4b2e2cad4
?>

Common Multi-Byte Encodings

Enabled via mbstring extension (mb_ functions):

EncodingRegionNotes
GB18030ChinaOfficial Chinese standard
GB2312ChinaLegacy Simplified Chinese
BIG5TaiwanTraditional Chinese
EUC-JPJapanUnix-style Japanese
Shift_JISJapanMicrosoft/Apple systems
EUC-KRKoreaUnix-style Korean
KOI8-RRussiaCyrillic (RFC 1489)
ISO-2022-JPJapanEmail/newsgroup encoding

Key Considerations