note 42038 modified in function.bin2hex by aidan

From: Date: Tue, 01 Jun 2004 07:06:29 +0000
Subject: note 42038 modified in function.bin2hex by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-70394@lists.php.net to get a copy of this message
hexdump() is a fantastic function for "dumping" packets or binary output from servers. See the below link for more information. http://ircphp.com/~imho/?file=function.hexdump.php A more simple version, bin2hex_string(), is a simple function to convert strings to readable hex data. <?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

« previous php.notes (#70394) next »