1

I'm trying to convert a "string" which is filled with values from "\x00" to "\xFF" and I would like to know if a special bit is set in a special byte in the string. Therefore I created a small function but it does not work:

 sub isBitSetInCtrMsg
 {
     my $data              = shift;   # value can be: "\xFF"
     my $data_byte_address = shift;   # value can be 0-88
     my $data_bit_address  = shift;   # value can be 0-7

     my @data_array = split('',$data);   #"split string into bytes"

     my $data_byte = $data_array[$data_byte_address];   # pick specific byte

     $is_set = ($data_byte & (1 << $data_bit_address)); # check if bit is set

     return $is_set;                                    # return value
 }

I thought it was quit a simple part of my code but the problem is if $data = 0xFF then everything works fine, but if my data is \xFF my code doesn't work.

Can anyone tell me why and how can I check this in the correct way?

1 Answer 1

1

Perl is magic when it comes to handling scalars. It decides on the fly how to 'handle' it.

How is the value passed into this subroutine? Because I think that's where your thing might be breaking.

Consider:

print 0xFF,"\n";
print "\xFF","\n";
print '\xFF',"\n";

In each case, perl's figuring out how to treat the string you've handed it. There's every chance that if you're passing it '\xFF' it's deciding that's 4 separate bytes, which'll be where your problem comes in.

Try adding:

use Data::Dumper;

And in your sub (before assigning):

print Dumper \@_;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.