Home  |  Linux  | Mysql  | PHP  | XML
From:John W. Krahn Date:Wed Mar 10 07:17:08 2010
Subject:Re: Reading BInary file.
John W. Krahn wrote:
> jis wrote:
>> Hello Perl Gurus.
>
> Hello,
>
>> I have been trying to read BInary file and have not found the best way
>> to parse it through.
>> I have been through perl forums and have not found a statisfactory
>> answer.
>>
>> Iam trying to read binary file (.bin) and read it to an array in hex
>> format bytewise.
>>
>> I have two problems.
>>
>> 1. Read the file to an array.
>> 2. Reading bytewise to array.
>>
>> I have used
>>
>> binmode FILE;
>> undef $/;
>> my $data = <FILE>;
>> close FILE;
>> my $hex = unpack 'H*', $data;
>>
>> Now I have got the file in a scalar variable.
>> I want to split the variable into an array where each array index
>> represents
>> one 1 byte hex value.
>>
>> I have tried to use split command. But it takes a long time.and I do
>> not how to split
>> byte wise.
>
> local $/;
>
> my @hex = map unpack( 'H2', $_ ), split //, <FILE>;

Or more simply as:

local $/;

my @hex = unpack '(H2)*', <FILE>;



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
Navigate in group perl.beginners at sever nntp.perl.org
Previous Next


Your recent visits
Windows Services and Logical Volume Monitoring Script ....
Re: 1-line text file with numbers, need to add to them
Re: 1-line text file with numbers, need to add to them
Re: running stats on a file
Re: Lingua::Stem 0.83 module installation problem



  
© No Copyright
You are free to use Anything, but please consult your advocate before doing so as this website
also list content from other sources which may be copyrighted.
Site Maintained by Zareef Ahmed
Powered By PHP Consultants