Displaying the byte sequence of a string in PHP

August 26th, 2009 by Jérôme Leave a reply »

Since I am never able to remember this, I decided to write it down here.

The code is the following :

function showByteSequence( $string )
{
 for ( $i = 0; $i < strlen( $string ); $i++ )
 {
 echo '0x', dechex( ord( $string[ $i ] ) ), ' ';
 }

 echo "\n";
}

For example writing

showByteSequence( 'foo-bar' );

should display

0x66 0x6f 0x6f 0x2d 0x62 0x61 0x72
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Advertisement

1 comment