Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-16 13:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found