Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Low level data manipulation in Perl

Reply
Thread Tools

Low level data manipulation in Perl

 
 
Leonard Challis
Guest
Posts: n/a
 
      01-16-2005
Hi everyone,

I have spent a few hours looking on Google, Perl.com, CPAN etc to try find
some information on messing about with low leveldata in Perl. I am talking
about opening files and looking at them in their very simplest format, 1s
and 0s.

What I have noticed from my searches so far is things like pack(), unpack(),
binmode() and some other stuff, but not really what I'm looking for, AFAIK.

If anyone could even just point me in the right direction, maybe just the
proper keywords to use in google to find some articles and tutorials on this
kind of task in Perl I would much appreciate it.

Thanks a lot,
Lenny Challis


 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      01-16-2005


Leonard Challis wrote:

> I have spent a few hours looking on Google, Perl.com, CPAN etc to try find
> some information on messing about with low leveldata in Perl. I am talking
> about opening files and looking at them in their very simplest format, 1s
> and 0s.
>
> What I have noticed from my searches so far is things like pack(), unpack(),
> binmode() and some other stuff, but not really what I'm looking for, AFAIK.


In what way are they not what you are looking at? Appart fromm those
three there's also vec().

 
Reply With Quote
 
 
 
 
Gregory Toomey
Guest
Posts: n/a
 
      01-16-2005
Leonard Challis wrote:

> Hi everyone,
>
> I have spent a few hours looking on Google, Perl.com, CPAN etc to try find
> some information on messing about with low leveldata in Perl. I am talking
> about opening files and looking at them in their very simplest format, 1s
> and 0s.
>
> What I have noticed from my searches so far is things like pack(),
> unpack(), binmode() and some other stuff, but not really what I'm looking
> for, AFAIK.
>
> If anyone could even just point me in the right direction, maybe just the
> proper keywords to use in google to find some articles and tutorials on
> this kind of task in Perl I would much appreciate it.
>
> Thanks a lot,
> Lenny Challis


Pack/unpack easily translate to hexadecimal, and conversion to binary from
there is trivial.

But Perl has the full complement of bit twiddling operators anyway (similar
to C).

gtoomey
 
Reply With Quote
 
Leonard Challis
Guest
Posts: n/a
 
      01-17-2005
What I am saying is this: Is there any way of opening a file, and instead of
getting the text in side it, you actually see the 1s and0s or the
hexnumbers?

This would then allow me to use the bitwise operators etc to manipulate it.

If there isn't, what would anyone suggest trying? For instance, could i
convert the ascii data i receive in to hex or binary. I'm guessing it isn't
just a simple case of converting it back right?

Thanks for any tips or hints, even a book to read
Cheers,


Lenny



"Gregory Toomey" <> wrote in message
news:...
> Leonard Challis wrote:
>
>> Hi everyone,
>>
>> I have spent a few hours looking on Google, Perl.com, CPAN etc to try
>> find
>> some information on messing about with low leveldata in Perl. I am
>> talking
>> about opening files and looking at them in their very simplest format, 1s
>> and 0s.
>>
>> What I have noticed from my searches so far is things like pack(),
>> unpack(), binmode() and some other stuff, but not really what I'm looking
>> for, AFAIK.
>>
>> If anyone could even just point me in the right direction, maybe just the
>> proper keywords to use in google to find some articles and tutorials on
>> this kind of task in Perl I would much appreciate it.
>>
>> Thanks a lot,
>> Lenny Challis

>
> Pack/unpack easily translate to hexadecimal, and conversion to binary from
> there is trivial.
>
> But Perl has the full complement of bit twiddling operators anyway
> (similar
> to C).
>
> gtoomey



 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      01-17-2005
"Leonard Challis" <> wrote:
> What I am saying is this: Is there any way of opening a file, and instead
> of getting the text in side it, you actually see the 1s and0s or the
> hexnumbers?


The text is identical to the 1 and 0s. I highly doubt that you are able
to stare at a CPU and "see" the insides of the Perl and perl programs. So
what you see is not what is inside perl, but rather what you told Perl to
print to your monitor. If you want to see 1s and 0s, then tell Perl to
print them as 1s and 0s.

> This would then allow me to use the bitwise operators etc to manipulate
> it.


Why don't you read the docs on those bitwise operators?

> If there isn't, what would anyone suggest trying? For instance, could i
> convert the ascii data i receive in to hex or binary. I'm guessing it
> isn't just a simple case of converting it back right?
>
> Thanks for any tips or hints, even a book to read


I think you've already been given those tips and hints. Please, go
followup on them. After you read all of perlop and all of perlfunc, if
you don't understand, come back and ask specific questions about the
operators and functions you don't understand.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-17-2005
Leonard Challis <> wrote in comp.lang.perl.misc:
> What I am saying is this: Is there any way of opening a file, and instead of
> getting the text in side it, you actually see the 1s and0s or the
> hexnumbers?


The text *is* the ones and zeroes, it's just a matter of interpretation.
To access individual bits, use vec() (perldoc -f vec).

> This would then allow me to use the bitwise operators etc to manipulate it.


What is stopping you?

Here is an example: Compare the strings "a" and "A" bitwise and show the
bit number(s) where they differ:

my ( $lower, $upper) = ( 'a', 'A');
for ( 0 .. 7 ) { # eight bits
print "bits $_ differ\n" if vec( $lower, $_, 1) != vec( $upper, $_, 1);
}

For ASCII code, that prints "bits 5 differ". Check with an ASCII table.

Anno
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-17-2005
"Leonard Challis" <> wrote in
news:csgpvs$q6d$:

[ Please do not top-post. If you do not know what that means, and even if
you do, please read the posting guidelines for this group. ]

> What I am saying is this: Is there any way of opening a file, and
> instead of getting the text in side it, you actually see the 1s and0s
> or the hexnumbers?
>
> This would then allow me to use the bitwise operators etc to
> manipulate it.


You are misguided.

See

perldoc perlop

Sinan.
 
Reply With Quote
 
Leonard Challis
Guest
Posts: n/a
 
      01-17-2005
You don'tseem to get me straight...

> "Leonard Challis" <> wrote:
>> What I am saying is this: Is there any way of opening a file, and instead
>> of getting the text in side it, you actually see the 1s and0s or the
>> hexnumbers?


> The text is identical to the 1 and 0s. I highly doubt that you are able
> to stare at a CPU and "see" the insides of the Perl and perl programs. So
> what you see is not what is inside perl, but rather what you told Perl to
> print to your monitor. If you want to see 1s and 0s, then tell Perl to
> print them as 1s and 0s.


I don't want to look at perl's code, I just want to see the file I open. Say
for instance I have an image and I open it in a HEX editor, I see the binary
or hex as well as the text for that file. I was wondering if there was any
way of opening the file and inputting it in this format instead of doing a
lot of converting. I know you can convert stuff, google has plenty of stuff.

>> This would then allow me to use the bitwise operators etc to manipulate
>> it.

>
> Why don't you read the docs on those bitwise operators?


I know, i was just stating thats what I was planning on doing so you had
more information. I have been reading up on them.

>
>> If there isn't, what would anyone suggest trying? For instance, could i
>> convert the ascii data i receive in to hex or binary. I'm guessing it
>> isn't just a simple case of converting it back right?
>>
>> Thanks for any tips or hints, even a book to read

>
> I think you've already been given those tips and hints. Please, go
> followup on them. After you read all of perlop and all of perlfunc, if
> you don't understand, come back and ask specific questions about the
> operators and functions you don't understand.


I have been toldabout conversion to hex/binary and about bitwise operators..
NOT about opening a file and inputting in this format straight away. If you
don'thave an answer I don't mind, don't moan.

Lenny Challis


 
Reply With Quote
 
Leonard Challis
Guest
Posts: n/a
 
      01-17-2005
Awesome.

This is exactly what I have been wondering, whether converting from the
ASCII representation would give me what it is in Binary/HEXetc, and I have
been reading about vec() too, it's pretty cool!

Thanks alot for your example,
Lenny Challis

"Anno Siegel" <> wrote in message
news:csgsih$t3v$...
> Leonard Challis <> wrote in comp.lang.perl.misc:
>> What I am saying is this: Is there any way of opening a file, and instead
>> of
>> getting the text in side it, you actually see the 1s and0s or the
>> hexnumbers?

>
> The text *is* the ones and zeroes, it's just a matter of interpretation.
> To access individual bits, use vec() (perldoc -f vec).
>
>> This would then allow me to use the bitwise operators etc to manipulate
>> it.

>
> What is stopping you?
>
> Here is an example: Compare the strings "a" and "A" bitwise and show the
> bit number(s) where they differ:
>
> my ( $lower, $upper) = ( 'a', 'A');
> for ( 0 .. 7 ) { # eight bits
> print "bits $_ differ\n" if vec( $lower, $_, 1) != vec( $upper, $_,
> 1);
> }
>
> For ASCII code, that prints "bits 5 differ". Check with an ASCII table.
>
> Anno



 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-17-2005
"Leonard Challis" <> wrote in
news:csgvju$413$:

>> "Leonard Challis" <> wrote:
>>> What I am saying is this: Is there any way of opening a file, and
>>> instead of getting the text in side it, you actually see the 1s
>>> and0s or the hexnumbers?


....

> I have been toldabout conversion to hex/binary and about bitwise
> operators.. NOT about opening a file and inputting in this format
> straight away. If you don'thave an answer I don't mind, don't moan.


WTF are you talking about? There is no excuse for the problem
description above unless you are six years old.

#! /usr/bin/perl

use strict;
use warnings;

use File::Slurp;

my $file = shift or die "Provide an input file name.\n";
my $contents = read_file($file, binmode => ':raw');

use constant CHUNK_SIZE => 0x10;

my $pos = 0;
my $last = length $contents;

while($pos + CHUNK_SIZE < $last) {
print hexdump_line(substr $contents, $pos, CHUNK_SIZE);
$pos += CHUNK_SIZE;
}

print hexdump_line(substr $contents, $pos, $last - $pos) if($pos <
$last);

sub hexdump_line {
my ($row) = @_;
my @bytes = split '', $row;
my $output;
$output .= sprintf '%2.2X', ord($_) for @bytes;
$row =~ s/[^\x20-\x7e]/\./g;
$output .= "\t$row\n";
}
__END__

C:\Home> 050117-b.pl 050117-b.pl
2321202F7573722F62696E2F7065726C #! /usr/bin/perl
0D0A0D0A757365207374726963743B0D ....use strict;.
0A757365207761726E696E67733B0D0A .use warnings;..
0D0A7573652046696C653A3A536C7572 ..use File::Slur
703B0D0A0D0A6D79202466696C65203D p;....my $file =

Sinan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pcos low carb diet. Low carb diet program. Cholesterol low carb diet.Low carb diet foods. zyraco C++ 0 11-10-2009 01:44 PM
Low carb diabetes diet. Low fat high carb diet. The low carb diet.Low carb diet pregnancy. zyraco C++ 0 11-10-2009 01:44 PM
Low carb calorie diet. No low carb diet. Free low carb diet. Low carbdiet meal plan. zyraco C++ 0 11-10-2009 01:44 PM
Trying to design low level hard disk manipulation program ragtag99 C++ 13 09-22-2006 08:14 AM
c is a low-level language or neither low level nor high level language pabbu C Programming 8 11-07-2005 03:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57