note 42038 modified in function.bin2hex by aidan
| From: | aidan@php.net | Date: | Tue, 01 Jun 2004 06:30:09 +0000 |
| Subject: | note 42038 modified in function.bin2hex by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-70393@lists.php.net to get a copy of this message | ||
Below is my function to convert a binary string into a readable hexidecimal representation.
Perfect for debugging stream or socket output.
Function:
<?php
function bin2hex_string ($data)
{
// Properly converts a binary string to a hexidecimal string
function bin2hex_nibble ($nibble)
{
// binary to hexidecimal, then uppercase, then 0 pads
return sprintf("%02s", strtoupper(bin2hex($nibble)));
}
// Explodes, applies bin2hex_nibble, then implodes with spacing.
return implode(' ', array_map('bin2hex_nibble', str_split($data)));
}
?>
Usage example:
<?php
$binarystr= "\xFF\xFF\xFF\xFF";
print bin2hex_string($binarystr);
?>
Output:
FF FF FF FF
--was--
Below is my function to convert a binary string into a readable hexidecimal representation.
Perfect for debugging stream or socket output.
Function:
<?php
function bin2hex_string ($data)
{
// Properly converts a binary string to a hexidecimal string
function bin2hex_nibble ($nibble)
{
// binary to hexidecimal, then uppercase, then 0 pads
return sprintf("%02s", strtoupper(bin2hex($nibble)));
}
// Explodes, applies bin2hex_nibble, then implodes with spacing.
return implode(' ', array_map('bin2hex_nibble', str_split($data)));
}
?>
Usage example:
<?php
$binarystr= "\\xFF\\xFF\\xFF\\xFF";
print bin2hex_string($binarystr);
?>
Output:
FF FF FF FF
http://www.php.net/manual/en/function.bin2hex.php