Generating a hex/ascii (??) string from a file

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Generating a hex/ascii (??) string from a file

Post by Chris Corbyn »

Code: Select all

<img src="javascript: '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x0e\x00\x00\x00\x0e\x08\x03\x00\x00\x01_\x91\xedu\x00\x00\x00{PLTE\xbf\xbf\xbf\x94\x00(J\x00\x17\xc5z\x93\xc6\x008\xff\x0fYr\x00 \xce\x10I\xee\x00A\xff!b\x9c\x00(\xff\x8b\xd5r\x0f0J\x00\x0f\xc5z\x8b\xce\x008\xff\x0fQ\xff\x07Q\x9cJb\xac\x000\x84\x100Z\x00\x17\xfd\x00Q\xcdbz\xff1zZ\x00 \xbc\x008\xd6\x08A\xb4\x000\xd6\x00Ab\x00\x17\x84\x00(\xceBj\x8b0I\xcdb\x83\xfd\x00I\xbc\x000\xde\x00A\xff\xa5\xee\xe6\x00Aj\x00 2#\x8cX\x00\x00\x00\x01tRNS\x00@\xe6\xd8f\x00\x00\x00\x7fIDATx\x9c}\x8em\x0f\x820\x0c\x84\xbb\x82N\xeb\x04\x87v\xe0\x146\xe4\xf5\xff\xffB7\xc4\xe8\x07\xe3%\xed\x93K.\x97\x03\x08r\xf1\xd9pf\x82\x8f\x92\x8a\xc0\x9fqu\x99\xf4\x11R\xef\xed\x12\x9f\x19\xdb\xaf0x\x99E\xe4\x1b\x9d&\x81]u#\x8e\xad=]U\xe0cF\x84\x7f*kc\xba]Z\xaemF\x1f\xc6Sm\xc5e\xb1r\x98\x86\xfcN\xae\xe1\xd72A\xc2m\x9b#\x16\xef\xa9\x8aY\x15?Z\x9f\xce4\x06\x94\x17FcO\x00\x00\x00\x00IEND\xaeB`\x82'" />
I'm reading an article on displaying images on web pages by embedding the image data straight into the page.

The above line of code achieves this, where that uber long string in the src tag is just the binary data of the image file. How did the author get it into that format? I thought it may have been using `hexdump' but I can't figure it out.

Thanks,

d11

EDIT | Actually I see ascii escape sequences in that... still unsure how to arrive at that string from a raw image.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It seems that a modified version of this function submitted by a user to the manual works:

Code: Select all

function hexentities($str) {
   $return = '';
   for($i = 0; $i < strlen($str); $i++) {
       $return .= '&#x'.bin2hex(substr($str, $i, 1)).';'; //My version is '\\x'.bin2hex(substr($str, $i, 1));
   }
   return $return;
}
Post Reply