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):
Encoding | Region | Notes |
---|---|---|
GB18030 | China | Official Chinese standard |
GB2312 | China | Legacy Simplified Chinese |
BIG5 | Taiwan | Traditional Chinese |
EUC-JP | Japan | Unix-style Japanese |
Shift_JIS | Japan | Microsoft/Apple systems |
EUC-KR | Korea | Unix-style Korean |
KOI8-R | Russia | Cyrillic (RFC 1489) |
ISO-2022-JP | Japan | Email/newsgroup encoding |
Key Considerations
- bin2hex returns lowercase letters by default
- Add strtoupper() for uppercase hexadecimal output
- Hex pairs represent ASCII values for each character
- Binary-safe methods work with any input format