Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

how to convert ascii to bin

by bh_perl (Monk)
on Aug 19, 2008 at 02:29 UTC ( #705118=perlquestion: print w/ replies, xml ) Need Help??
bh_perl has asked for the wisdom of the Perl Monks concerning the following question:

hi.. Actually I want to convert binary data into hex value but I see that some of the values are not convert to HEX properly. Anybody could help me ?.. below is my code..

open (DATA, "$fname"); binmode DATA; while (read(DATA, $data, 280)!= 0) { my $hexval = unpack "H*", $data; $rec++; if (defined($decode) && $decode =~ /csv/) { csvout(uc($hexval)); } else { decode(uc($hexval)); } insertLine() if ($line); } close (DATA);
Otherwise, I am also want to convert back the binary to ASCII file... but how could i convert back ASCII to binary file in perl ?.. Anyone knows about this ?

20080820 Janitored by Corion: Changed PRE tags to P tags, as per Writeup Formatting Tips

Comment on how to convert ascii to bin
Download Code
Re: how to convert ascii to bin
by GrandFather (Cardinal) on Aug 19, 2008 at 02:38 UTC

    Your question is bereft of meaning. ASCII is a code for encoding characters. A "binary" file is a stream of bytes which may or may not contain ASCII encoded characters, although by convention is not a text file (which contains text generally as ASCII encoded characters). Do you perhaps mean that you have transformed a binary file into an ASCII representation of the hex values of the bytes and that you now wish to transform that ASCII representation back into binary?

    It may help if you show us a sample of the ASCII encoded input you wish to convert back to "binary".


    Perl reduces RSI - it saves typing
      Below sample of my ASCII data in hex value:- F9F988883FFBFBFBFB5151FBFB8CA636E2E21A1A0588888888F5F5A7A7A7A78888F9F9 +F9F93D3D3D3DB4B4A1A1888888888888888888888D8D88880A0A0B760A34900300000 +000000000FF000000020000000000000006021800181841FFFFFFFFFFFFFFFFFFFFFF +FFFFFFFFFFFF030F0000000000000000024019D3844219D3134019D30D4019D301000 +0000B0156601208051D0C230708051D0C231A140000000602062849755FFFFFFFFFFF +06021800181841FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0F0003000B760A3490030 +0000000000000FF000000020000000000000006021800181841FFFFFFFFFFFFFFFFFF +FFFFFFFFFFFFFFFF030F0000000000000000024019D3844219D3134019D30D4019D30 +100000

        You should have at the very least posted that in a code block so that it would be wrapped.

        And what do you think that represents?

        (Tip: you pasted an odd number (559) hex characters which means something is wrong somewhere!)


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: how to convert ascii to bin
by ww (Bishop) on Aug 19, 2008 at 12:56 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2014-09-21 20:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls