PHP convert string to hex and hex to string
When working with binary data in PHP, converting between human-readable strings and hexadecimal representations is a frequent requirement. However, developers often encounter discrepancies when using standard helper functions to reverse these conversions. A common stumbling block occurs when dealing with non-ASCII characters generated by operations like XOR encryption.
Consider a scenario where you have applied an XOR operation to the string "this is the test", resulting in a mixed-character output containing symbols and letters, such as âââ§Pâ§P â«Â§Tâ§â. If you attempt to convert this result back to hexadecimal using a standard loop-based function, and then try to reconstruct the original string from that hex data, you may find the output differs from your input.
The root cause lies in how PHP handles character encoding and byte manipulation. The provided examples often found online utilize ord() and chr(). While these functions work seamlessly with standard ASCII characters (bytes 0-127), they behave unpredictably when the string contains multi-byte UTF-8 characters or extended ASCII values.
The Flaw in Manual Loop-Based Conversion
The typical implementation for converting a string to hex looks like this:
function strToHex($string){
$hex='';
for ($i=0; $i < strlen($string); $i++){
$hex .= dechex(ord($string[$i]));
}
return $hex;
}
And the reverse conversion:
function hexToStr($hex){
$string='';
for ($i=0; $i < strlen($hex)-1; $i+=2){
$string .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $string;
}
The issue arises because ord() retrieves the byte value of a character at the current position. If your string contains characters outside the standard ASCII range (like the symbols in your XORed result), PHP treats them as multi-byte UTF-8 sequences. When you iterate with strlen(), you are counting code points, but if not handled carefully, you might be reading into the middle of a multi-byte character sequence or misinterpreting the byte order depending on your system's default encoding settings.
Furthermore, when converting back with chr(hexdec(...)), PHP assumes the result should be treated as a single-byte ASCII character by default. If your original data was intended to be a specific UTF-8 encoded byte that happens to be above 127, chr() will output it as an ISO-8859-1 or Latin-1 character rather than preserving the multi-byte sequence required for valid UTF-8. This mismatch leads to the garbled output you observed: âââ§Pâ§Pâ«Â§Tâ§q instead of matching your input exactly.
The Robust Solution: Pack and Bin2Hex
To solve this reliably, you must bypass manual looping entirely. PHP provides built-in functions that handle the underlying byte operations correctly regardless of the character encoding nuances.
For converting a string to hex, use pack() combined with bin2hex():
function strToHex($string) {
// pack('H*', $string) treats the input as a hex string first
// We need to convert the actual bytes of the string to a hex representation.
// However, bin2hex() expects a string and returns lowercase hex.
// To handle this correctly without encoding assumptions, we treat the string
// as raw bytes or ensure proper handling.
// The most direct way for arbitrary byte sequences:
return bin2hex($string);
}
However, bin2hex() works best on strings where each character represents a single byte in the internal encoding (often ISO-8859-1 for older PHP versions or depends on locale). If your data is truly binary (like XOR output), it is safer to ensure the string is treated as raw bytes.
For converting hex back to a string, use unpack():
function hexToStr($hex) {
// unpack('H*', $hex) interprets the input as a hex string and returns the binary string.
return unpack('H*', $hex)[1];
}
Wait, unpack('H*') expects a hex string to produce a string of bytes? No, that is incorrect usage for this direction. The correct unpacking format for converting hex string back to binary string is:
function hexToStr($hex) {
return pack('H*', trim($hex));
}
This approach treats the input $hex strictly as a sequence of hexadecimal digits representing raw bytes. pack('H*', ...) interprets two hex characters as one byte and constructs the resulting string based on that byte value. This method completely avoids the pitfalls of ord() and chr(), ensuring that multi-byte UTF-8 sequences or extended ASCII values are reconstructed exactly as intended.
Implementation Considerations
When implementing this in a larger application, remember to trim any leading or trailing whitespace from your hex string before processing, as pack will interpret spaces or newlines if present. Additionally, ensure that the input hex string contains only valid hexadecimal characters (0-9, a-f, A-F).
If you are working within the Laravel ecosystem, these utility functions can often be abstracted into a helper class or service provider, keeping your core business logic clean. For example, you might define these methods in a dedicated StringHelper trait to ensure consistency across your codebase, avoiding reinvention of these specific conversion patterns.
By leveraging pack('H*') and bin2hex(), you ensure that the conversion is byte-accurate, handling any character set generated by cryptographic operations like XOR without data corruption.