Processing Bit Strings

M

Matt Taylor

I have a script which runs postprocessing on some data and formats the
results into a table. This is compiled into a C project. The perl script
creates an 800x800 matrix of bits (~800 KB). After creating an enormously
long string, it formats it for C source as follows:

# Pad out the end of the bit string to a byte
$bitstr .= '0' x ((8 - (length($bitstr) % 8)) % 8);

# Create an array of bytes
@bytes = ();
while(length($bitstr) > 0)
{
push(@bytes, sprintf('0x%02X', ord(pack('b8', substr($bitstr, 0, 8)))));
$bitstr = substr($bitstr, 8);
}

# Down here I have code to print the array

This takes several minutes to run under perl 5.8 on my Pentium 4 2.53 GHz. I
was wondering if there was a faster way to do this? Several minutes is
usually acceptable, but in a debug cycle I run this script frequently, and
it would make my life much easier if it were faster.

-Matt
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top