Sunday, December 4, 2011

How do I get Perl to convert a binary file into a (decimal) number and back again?

How do I get Perl to convert a file into one large base10 number? I can get Perl to slurp a binary file (such as a .gif) into a string variable, but it's treated as a string. How do I turn that string into a single large number I can then multiply, subtract, etc? And then vice versa.





Conversely, if that's not possible, can I somehow read a file and output it as a textfile containing a large decimal number? I can't figure this one out...|||Well, there are a bunch of Digest:: modules which take any string and "hash" it to a pseudo-unique integer. And there is the Math::BigInt module which lets you do calculations with any size numbers. It depends on what you're really trying to do; e.g. do you need to convert the "large integer" back to a GIF file? Or is a one-way hash sufficient? etc.

No comments:

Post a Comment