pack and unpack integer data

K

Kim Schulz

Hi I'm working on doing a bit of simple compression of some datafiles I
have.
Right now I have files like:
1 2 5 4 3 23 6 5 34 455 6 3 22 1 1 1 22 .... etc.

I would like to read the numbers one by one (no problem) and then pack
them into the smalles binary representation I can get.

The idea is that I later on would like to be able to unpack them again.

I have been looking at pack/unpack and tried pack("b*", $num) where
$num is one of the numbers "represented as a string".
That seems to pack it fine, but then I want to unpack it again.
And cant seem to get that one working like it is supposed to.

Can anyone point me in the right direction? (yes I have looked at the
perldoc for unpack and pack but what I understood to be correct does
not work).
 
X

xhoster

Kim Schulz said:
Hi I'm working on doing a bit of simple compression of some datafiles I
have.

Have you considered using a general purpose module/tool (like gzip)?
Right now I have files like:
1 2 5 4 3 23 6 5 34 455 6 3 22 1 1 1 22 .... etc.

I would like to read the numbers one by one (no problem) and then pack
them into the smalles binary representation I can get.

How do you know what the smallest representation will be if you don't know
what the largest possible number you will see will be?
The idea is that I later on would like to be able to unpack them again.

I have been looking at pack/unpack and tried pack("b*", $num) where
$num is one of the numbers "represented as a string".

That will ignore all but the least significant bit of every byte
(character?) in the sting representation of $num. Hardly what you want.
That seems to pack it fine, but then I want to unpack it again.

On what basis do you say that it seems to pack it fine? If you can't get
it unpacked, how do you know if the error is in the packing or the
unpacking?
And cant seem to get that one working like it is supposed to.

We don't know how you think it is supposed to work.
Can anyone point me in the right direction?

Post the code that does both the packing and unpacking, the input, the
actual output, and what you thought the output would be.
(yes I have looked at the
perldoc for unpack and pack but what I understood to be correct does
not work).

You misunderstood something. If you showed is the code, or gave us some
indication of your understanding, perhaps we could help you figure out what
it is you misunderstood.

Xho
 
K

Kim Schulz

Hi I'm working on doing a bit of simple compression of some
datafiles I have.

Have you considered using a general purpose module/tool (like gzip)?[/QUOTE]

Yes, but this is for the purpose of learning only, so that would be the
easy way out.

How do you know what the smallest representation will be if you don't
know what the largest possible number you will see will be?

in generel it is bound by a parameter I have which currently is found
by finding the largest number in the file.
That will ignore all but the least significant bit of every byte
(character?) in the sting representation of $num. Hardly what you
want.

On what basis do you say that it seems to pack it fine? If you can't
get it unpacked, how do you know if the error is in the packing or the
unpacking?

you are right. and thats why I asked.
We don't know how you think it is supposed to work.


Post the code that does both the packing and unpacking, the input, the
actual output, and what you thought the output would be.

You misunderstood something. If you showed is the code, or gave us
some indication of your understanding, perhaps we could help you
figure out what it is you misunderstood.

I will do that when Im back on my laptop again.
 
K

Kim Schulz

Unless you are doing this as a learning exercise, there is no need to
re-invent the wheel. Data compression is a well-studied subject. zlib
is a good, free implementation of a good compression algorithm. The
Compress::Zlib module, available on CPAN, gives a Perl program access
to zlib. There are also many other Compress:: modules on CPAN. One of
them will surely satisfy your requirements.

yep....it's for the purpose of learning.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top