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

Re: Creating a binary file problem

by Eliya (Vicar)
on Apr 05, 2012 at 17:22 UTC ( [id://963718]=note: print w/replies, xml ) Need Help??


in reply to Creating a binary file problem

It's not entirely clear to me what exactly you want to be written to the file, but in case you want to print the values expressed in the hex strings (such as "0x0000001b") as binary values to the file, you could say

my $hex = "0x0000001b"; print BIN pack('H*', substr($hex,2));

This would also work for values larger than a Perl number could hold.  In the example above, it would write 4 bytes / 32 bits.

On a side note, don't use printf, unless you actually specify a format string.

Replies are listed 'Best First'.
Re^2: Creating a binary file problem
by ikegami (Patriarch) on Apr 05, 2012 at 20:46 UTC
    Given his use of pack 'C', I imagine he wants the single byte 1B, so
    print BIN pack('C', hex($hex));

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found