Calculation method of phpcrc verification code (PHP exclusive or)

Source: Internet
Author: User
Tags printable characters
Calculate the phpcrc verification code (PHP exclusive or). For example, if I have a segment string named "139c", use a calculator to calculate the verification code, copy 13 ^ 9c = to the calculator and calculate 8F in hexadecimal notation.

PHP computing:

Echo dechex (0x13 ^ 0x9c); // The result is 8f, correct echo ""; $ s1 = '0x13 '; $ s2 = '0x9c '; echo dechex ($ s1 ^ $ s2); // The result is 0. the error echo ""; $ s3 = '13'; $ s4 = '9c '; echo dechex ($ s3 ^ $ s4); // The result is 0, error



Assigning a value to a variable makes the calculation result different from that written directly... Please give us some advice or give a usable computing function


Reply to discussion (solution)

PHP and or operations, if both the front and back are strings, are performed using the asc code or

$ S1 = '0x13 ';
$ S2 = '0x9c ';

Convert to asc for comparison with or
'0' 'X' '1' '3'
00110000 01111000 00110001 00110011
^
00110000 01111000 00111001 01100011
----------------------------------------------------------------
00000000 00000000 00001000 01010000

The result is var_dump (chr (0). chr (0). chr (8). chr (80) should be string (4) "P" because three non-printable characters are used.


The dechex conversion starts from numeric characters (ASC does not recognize any other characters from 48 to 57) to the tail or non-numeric characters.
So the result is 0.

$s1 = 0x13;$s2 = 0x9c;echo dechex($s1^$s2);
8f
No problem

$ S1 = '0x13 ';
$ S2 = '0x9c ';
Is the character string 0x into the character

$ S1 = 0x13;
$ S2 = 0x9c;
These two are hexadecimal numbers. of course they are different.

$s1 = hexdec('0x13');$s2 = hexdec('0x9c');echo dechex($s1^$s2); // 8F$s3 = hexdec('13');$s4 = hexdec('9c');echo dechex($s3^$s4); // 8F

This is normal.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.