I've been banging my head around this question for the past few hours; there's a lot of similar questions around here, but nothing quite the same, and none of the techniques I've seen seem to be working.
I have a sequence of bytes (integers) that I've generated from input in my program - each one represent a red, green or blue color value of a pixel in a BMP image. I essentially need to extract the bitstream representation of each byte; that is, the binary sequence of that byte.
I've been using lots of different variations of pack()
and unpack()
, but I'm not coming out with proper results.
For instance:
sub convertToBinary {
my $str = unpack("B32", pack("N", shift));
return $str;
}
I've also tried:
my $str = unpack("b8", shift);
,
my $str = unpack("B8", shift);
,
my $str = unpack("b*", shift);
And numerous other variations; none of them are seem to be working. I don't feel like it should be too hard to extract the bitpattern of a byte though.. just eight '1's or '0's, right?
What am I missing here?
&
to check if a bit is set. That said, you're still incredibly vague about your input$offset = unpack("L", substr($bmp, 10, 4));
to find where the actual pixel data begins. So it's a string to begin with. Then, within afor
loop, I use$byte = unpack("CCC",substr($bmp, $offset + $counter, 1));
to pull out each byte - so I end up with a three-character (or 3 digit, I'm not sure exactly) value that represents the amount of red, green, or blue of a certain pixel. I'm trying the byte that contains that value into a sequence of bits, so I can look for the hidden characters