Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: bin2ascii and ascii2bin conversion

by Anonymous Monk
on Aug 03, 2001 at 16:04 UTC ( [id://101950]=note: print w/replies, xml ) Need Help??


in reply to bin2ascii and ascii2bin conversion

Here is a script I wrote to convert binary files to C/C++ include files. It uses ord to convert from binary to a hex string.
#!/usr/bin/perl -w $source = $ARGV[0]; $dest = $ARGV[1]; unless($source and $dest) { print "Usage: perl bin2hex sourcefile destfile\n"; exit 0; } $/=\1; open SRC, "$source"; open DST, ">$dest"; if (defined(SRC) and defined(DST)) { print DST "unsigned char data[] = {\n "; $n=0; while (<SRC>) { if ($n==8) {$n=0; print DST "\n ";} $toprint = ord $_; if ($toprint < 16) {printf DST "0x0%x, ",$toprint;} else {printf DST "0x%x, ",$toprint;} $n++; } print DST "\n}"; close SRC; close DST; }
Hope it helps. Chilliwilli.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://101950]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-28 22:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found