in reply to Binary Reading Questions
You will want to look at the pack and unpack functions if you are routinely converting between numbers and their binary representation as characters.
Usually, Perl does the Right Thing, but if you want to be safe from Unicode encodings, use the binmode() call after opening your file, so you get full control over what Perl writes to the file:
open my $fh, "<", $fname or die "Couldn't open $fname: $!"; binmode $fh; seek $fh, 258, 0; read $fh, (my $buf), 1; $buf = unpack "c", $buf; ...
Why do you want to convert the stuff from the character to the integer at all? If there are no calculations to be made, you will likely be faster by not converting at all. But still, the IO overhead of reading the file will likely dwarf any optimizations you might make in your code ...
Update: PodMaster pointed me to Pack/Unpack Tutorial (aka How the System Stores Data) by pfaut - you should read that tutorial.
Update 2: frodo72 pointed out a typo in binmmode - thanks frodo72!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Binary Reading Questions
by Eyeth (Sexton) on Jun 30, 2005 at 14:06 UTC | |
by idsfa (Vicar) on Jun 30, 2005 at 14:25 UTC | |
by Eyeth (Sexton) on Jun 30, 2005 at 15:57 UTC | |
|
Re^2: Binary Reading Questions
by BrowserUk (Patriarch) on Jun 30, 2005 at 13:17 UTC | |
by Corion (Patriarch) on Jun 30, 2005 at 14:40 UTC |