> I want to convert int(255) to a string of 8 ones (11111111). I have been
> trying to use unpack, but am missing something basic I think.
>
> I was also playing with the following with less than successful results.
> What am I doing wrong? One guess was the the @mask elements are string
> values and the bitwise operation are not performing as I initially
expected.

One way to do it is to treat it as a vector, which is exceptionally useful
if you intend to do anything based on whether or not specific bits are set.

Firstly, you'll need to convert the value into the actual binary
representation you're looking for.  Which means you need to _pack_ not
_unpack_ (if you had 8 bits, and you wanted to get '255' from it, you'd use
_unpack_ then.).

my @values = (255,200,187,15);
my @vectors;

foreach my $set (@values) {

        # 8 bits is a character, since you go to 255, it has to be unsigned

    push(@vectors, pack('C',$set));

    }

    # now, @vectors holds four binary values which can be used as vectors

foreach my $pos (0..$#vectors) {

    my $value = $values[$pos];
    my $vec = $vectors[$pos];

    my $bit_str = '';

    foreach (0..7) {

            # get the 1-bit value from the current position, bit $_

        my $cur_val = vec($vec,$_,1);
        $bit_str .= $cur_val;

        print("In Value $value, Bit $_ is: $cur_val\n");

        }

    print("\tThe Whole Bit Range for $value is: $bit_str\n");

    }

HTH,

!c

C. Church
http://www.digitalKOMA.com/church/
http://www.DroneColony.com/


_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to