Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How to convert a binary character to readable ASCII Integers

by w3ntp (Beadle)
on Feb 12, 2008 at 20:23 UTC ( #667665=perlquestion: print w/ replies, xml ) Need Help??
w3ntp has asked for the wisdom of the Perl Monks concerning the following question:

I am looking for a way to convert a binary character to an ascii readable integer. For example a "\x00" would be a "0" and a "\x01" would translate to a "1" and so on. If I create an array as follows: @array=("\x00", "\x01", "\x02"); and I write this to a text file, the hex dump looks like the following: "00 78 01 78 02 78". I am not sure where the "\x78" is coming from. I am running perl 5.8.8 on a Solaris 9 platform. Any ideas? I have tried the hex() and tr// functions without any success. I also found that doing math on binary data does not work?? thanks w3ntp

Comment on How to convert a binary character to readable ASCII Integers
Re: How to convert a binary character to readable ASCII Integers
by Fletch (Chancellor) on Feb 12, 2008 at 20:27 UTC

    Not that the original question's all that clear, but it sounds like you're looking for either chr, or possibly an invocation of unpack "c*" to turn characters into the corresponding integer values. If that's not it you need to show some actual code and give a better explanation than "doesn't work" (see also How (Not) To Ask A Question).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: How to convert a binary character to readable ASCII Integers
by kyle (Abbot) on Feb 12, 2008 at 20:32 UTC

    I think you're looking for ord. That will give you a number, which you can treat as a string if you want to, and Perl will convert it for you.

    my @array=("\x00", "\x01", "\x02"); print join q{, }, map { ord } @array; print "\n"; __END__ 0, 1, 2
Re: How to convert a binary character to readable ASCII Integers
by apl (Monsignor) on Feb 12, 2008 at 20:42 UTC
    78 hex is lower-case 'X'. When you write out "\x" you're asking to explicitly have lower-case 'X' written.

      ORLY?

      $ perl -MO=Deparse -e 'print "\x"' print "\000";

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        I followed the link, and I thank you for it. Nonetheless, 78 hex is lower-case 'X'. They got into the file somehow. (I'd like to see a little more of the code, myself.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://667665]
Approved by kyle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2015-07-05 20:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls