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.


In reply to Re: Re: Help with raw data. by graff
in thread Help with raw data. by samgold

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.