Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

anding 2 binary values

Status
Not open for further replies.

rotis23

Programmer
Aug 29, 2002
121
GB
Sorry - more questions on binary stuff.

I've got a 12 byte value obtained using:
Code:
$in_bin = unpack("H2" x 12,$input_from_socket);
I want to be able to AND (assign not test) it with another value say:
Code:
@main_bin = (0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01);
I'm not sure how to use the & operator in Perl - I assume I can't do:
Code:
$i=0;
foreach $byte (@bin_out)
{
   $main_bin[$i] = $main_bin[$i] & $byte;
   $i++;
}

because main_bin isn't a hex string?

I also need to convert the output back to a 96 bit binary value so that I can dump it into a database (mysql char[12] binary). Is there a way I can AND the 2 values without converting to a hex array - cut out the middle man?

I know Perl isn't the best for this sort of thing but I'm constrained for other reasons.

Thanks for your help,

rotis23


 
um...cant you just go:

Code:
$in_bin  ="001100110011";
$main_bin="000000000001";
$out = $in_bin & $main_bin;
print "out is $out\n";

for example?
 
That works, but the values are raw binary and not strings.
 
I don't think this is going to do what you expect:
Code:
$in_bin = unpack("H2" x 12,$input_from_socket);

That creates 12 bytes from $input_from_socket but only the first gets assigned to $in_bin. You have two options:
Code:
# one scalar with 12 bytes
$in_bin = unpack("H24",$input_from_socket);

# an array with 12 1-byte elements
@in_bin = unpack("H2" x 12,$input_from_socket);

If you use the array approach, you can AND them using:
Code:
for (1..12) {
   $main_bin[$_] &= $in_bin[$_];
}

I think I understand what you're asking but am not certain.
 
Thanks ishnid (and cadguy), the problem was the way I was printing debug info (i.e. pack and unpack again)

tester:
Code:
@one = (0x10,0x01);
@two = (0x32,0x01);

for(0..1)
{
        print unpack("H2",pack("C*",$one[$_]));
}

print "\n";

for(0..1)
{
        $one[$_] &= $two[$_];
        print unpack("H2",pack("C*",$one[$_]));
}

print "\n";

for(0..1)
{
        print unpack("B8",pack("C*",$one[$_]));
}

print "\n";
 
One more question though:

I need to convert the ANDed value back into a byte stream so that I can do an insert or raw data into a char[12] binary MySQL field.

Now, I have an array of 12 bytes that I need to merge together. I can't do:
Code:
for(0..11)
{
   $mysql_input = $mysql_input . $main_bin[$_];
}
because that turns it into a string, doesn't it?
 
The answer:
Code:
$input = pack("C12",@$cat[0],@$cat[1],@$cat[2],@$cat[3],@$cat[4],@$cat[5]
                                        ,@$cat[6],@$cat[7],@$cat[8],@$cat[9],@$cat[10],@$cat[11]);
I think I'm getting the hang of pack and unpack now!!
 
Seems like a lot of typing. This should work (untested):
Code:
$input = pack("C12", @{$cat});
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top