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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create a hex array

Status
Not open for further replies.

rotis23

Programmer
Aug 29, 2002
121
GB
Hi All,

How do I do the following C in Perl:
Code:
int res = 0;

char mask[] =      {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
char test[] =      {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};


res = ((mask[11] & test[11])
                        ||(mask[10] & test[10])
                        ||(mask[9] & test[9])
                        ||(mask[8] & test[8])
                        ||(mask[7] & test[7])
                        ||(mask[6] & test[6])
                        ||(mask[5] & test[5])
                        ||(mask[4] & test[4])
                        ||(mask[3] & test[3])
                        ||(mask[2] & test[2])
                        ||(mask[1] & test[1])
                        ||(mask[0] & test[0]) );

Create 2 12 bytes hex strings and mask them.

Thanks for any help,

rotis23
 
Look at the pack function for further info on packing hex strings

HTH
--Paul
 
I think the following should behave the same in Perl as in your C code. Seems to be just syntax that needs changing.
Code:
my $res = 0;

my @mask = (0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01);
my @test = (0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01);

$res = (($mask[11] & $test[11])
                        ||($mask[10] & $test[10])
                        ||($mask[9] & $test[9])
                        ||($mask[8] & $test[8])
                        ||($mask[7] & $test[7])
                        ||($mask[6] & $test[6])
                        ||($mask[5] & $test[5])
                        ||($mask[4] & $test[4])
                        ||($mask[3] & $test[3])
                        ||($mask[2] & $test[2])
                        ||($mask[1] & $test[1])
                        ||($mask[0] & $test[0]) );

One thing worth noting though, if you're not familiar with Perl, is the behaviour of the '||' operator. C returns 1 for true and 0 for false, whereas Perl returns the first true value encountered (or the last false value if they're all false), i.e.
Code:
// in C
int result = 4 || 5; // result is 1 (for true)
# in perl
my $result = 4 || 5; # $result is 4 (which is a true value)
It probably won't make any difference but I thought I'd mention it just in case.
 
I've been battling with pack and unpack for about 2 weeks now - still haven't got my head round it!!

The following code gives $res as no ouput.

Is the because:

1 - mask and test should really be in binary format
2 - i'm not using the pack function correctly
3 - i'm not using perl bitwise & properly

Code:
@mask = pack("H2" x 12,"\x01","\x00","\x00","\x00","\x00","\x00","\x00","\x00","\x00","\x00","\x00","\x00");
@test = pack("H2" x 12,"\x01","\x00","\x00","\x00","\x00","\x00","\x00","\x00","\x00","\x00","\x00","\x00");

$res = (($mask[11] & $test[11])
                        ||($mask[10] & $test[10])
                        ||($mask[9] & $test[9])
                        ||($mask[8] & $test[8])
                        ||($mask[7] & $test[7])
                        ||($mask[6] & $test[6])
                        ||($mask[5] & $test[5])
                        ||($mask[4] & $test[4])
                        ||($mask[3] & $test[3])
                        ||($mask[2] & $test[2])
                        ||($mask[1] & $test[1])
                        ||($mask[0] & $test[0]));


print "res = $res\n";
 
Thanks ishnid - that works - I was trying too hard.

I now have to convert (using pack and unpack I assume) 12 bytes from a socket into @test.

Any ideas?
 
OK sorted:
Code:
@urlAttr = unpack("H2" x 12,$input);
If the data is transmitted as binary.

2 more questions though:

1 - ishnad was right about the || returning the value rather than true. Is their a logical OR in perl I can use?

2 - Is it safe to transmit the data in binary?
 
'||' is the logical OR operator. Logically, it works exactly as C's does.

Whereas C returns a particular true value if the entire expression evaluates to true (i.e. 1), Perl will return the first true value it sees. This is useful in some situations. If you don't need this feature, you can happily ignore it and test for truth in the normal fashion:
Code:
my $res = 4 || 5; # $res is 4
if ($res) { # 4 is a true value so execute the block
   print "4 is true";
}
If you only need $res for testing truth, the only reason you'd need to know about ||'s return value is to avoid doing this (which would work in C, although long-winded):
Code:
#in perl
$res = 4 || 5; # $res is 4
if ($res == 1) { # 4 != 1 so don't enter the block
   print "4 equals 1? - I don't think so!";
}

// in c
res = 4 || 5; # res is 1
if (res == 1) { # indeed it is, so execute the block
   cout << &quot;no problem&quot;;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top