1

We are trying to convert a VB system into a web based system using PHP. Currently we are unable to understand few lines in VB which make us hard to convert it to php.

This is my VB code:

Dim data1 As Byte
Dim data1 As Byte
Dim num = 5117    
data1 = (num >> 6) And &HFF
data2 = num And &H3F

This is my php code:

$data1 = ($num >> 6) && &HFF;
$data2 = $num && &H3F;

Now the problem is, when we run the VB code and output it shows is different than in php. For instance in vb the data2 shows as 61 but in php it shows as 1. Can anyone help us to solve this problem.

2 Answers 2

1

I believe in PHP that the bitwise AND operator is & and not &&. And hex literals are not specified like that. Try something like this:

$data1 = ($num >> 6) & 0xFF;
$data2 = $num & 0x3F;

Check the PHP documentation for the bitwise operators. And for hex literals.

Sign up to request clarification or add additional context in comments.

Comments

0

In PHP &HFF and &H3F will not be considered as Hexadecimal code as you defined.

$data1 = ($num >> 6) && &HFF;
$data2 = $num && &H3F;

will generate error unexpected & .. You should try with -

$data1 = ($num >> 6) && hexdec ('&HFF');
$data2 = $num && hexdec ('&H3F');  

hexdec() will convert hexadecimal to decimal.

But in this case - $num && hexdec ('&H3F') will return true. So $data1 & $data2 will contain true. If you echo them you will get 1.

3 Comments

We have tried your method and still unable to match the answer we get in vb. We are getting 1 whenever we echo the answer. Is this right in PHP? @b0s3
Yes. Check the update. If explaing what actually you want to perform may be we can find some workaround.
@user3688754 - The original VB code is not doing a logical operation. It is doing a bitwise operation. In PHP the bitwise AND operator is &

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.