Unfortunitally unpack and ord are not the answer.
Um... what was the question? Based on the original post, I thought it was:
How can I convert that from raw to hex or raw to decimal?
So now the next question is: if you think unpack and ord are not the answer, why not? What did you try, what did you hope to get, and what did you actually get instead? Show us some code.
Based on the original question, I would think something like this would do:
# assume $sql contains an sql statement read from a file;
# we want to print the statement, showing visible ascii
# characters as-is, and converting other things to hex:
@bytes = split //, $sql;
for ( @bytes ) {
if ( /[\x21-\x7e]/ ) { print " $_"; }
else { printf( " %0.2x", ord ); }
}
YMMV, depending on what OS and Perl version you're using... e.g. Redhat 8 (9?) combined with Perl 5.8.0 might need some special pragmas to do this right (use bytes; no utf8; maybe binmode ":raw" on the file handle(s) involved).
Note that the above will show things like " 20 " for spaces, " 0a " for line-feeds, etc; just add stuff into the character class for "printable ascii" characters if you want whitespace printed out as-as. |