p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Converting hex to ASCII


Message #1 by markpenny@e... on Wed, 29 May 2002 14:47:03
Hi,

Can anyone help with the following:

I have some hexadecimal encoded data which needs converting to ASCII. The 
data is part of a MySQL field in a table. I can retrieve the data OK, but 
is it possible to convert the data as it stands to ASCII using a PHP 
function or two?

An example of the data is as follows: (it is the header data of an alert 
logged by Snort 1.8.3. Presumably, the each pair of characters represents 
a particular ASCII code. In the example, the data begins 47, 45, 54, 20 
etc.):

474554202F736372697074732F726F6F742E6578653F2F632B64697220485454502F312E300
D0A486F73743A207777770D0A436F6E6E6E656374696F6E3A20636C6F73650D0A0D0A

All help gratefully received!

Regards,

Mark.
Message #2 by markpenny@e... on Wed, 29 May 2002 18:03:23
Hi again,

I've sorted this out and am posting it here in case it helps anyone else.

The command needed is pack(format code, string). It takes a bunch of 
format codes and then the data to convert. There is a long list of format 
codes and the ways they are used in the PHP manual.

To convert my date the command was as follows:

pack("H*", $row[0]);

Where "H*" represents 'Hex string, high nibble first' and the * 
represents 'parse to the end of the data'. '$row[0]' is the data as pulled 
from a MySQL query.

Hope this helps someone else!

Regards,

Mark.

  Return to Index