Home > Backend Development > PHP Tutorial > PHP Socket编程 异或运算

PHP Socket编程 异或运算

WBOY
Release: 2016-06-13 12:21:55
Original
1360 people have browsed it

PHP Socket编程 异或运算 求助

如图,在做Socket编程,用到了异或运算,部分代码附后。但是现在遇到了这样的一个问题,图片中的是一个异或运算的结果,计算出来的东西echo出来是一些笑脸,如果仔细看的话这个笑脸代表1,空白的位置是0。XOR是自己计算的值,REVC_XOR是收到的异或值(认为是正确的)。长度是8位。
 请大神告知正确的异或方法代码或者如何修复这个问题。

<br />	$check = str_split($verify_data, 2);  // 将16进制数据转换成两个一组的数组<br />	for ($j = 0; $j < count($check); $j++) {<br />		$data = str_pad(base_convert($check[$j],16,2),8,0,STR_PAD_LEFT);<br />		$xor = $xor ^ $data;<br />		$xor = str_pad($xor,8,0,STR_PAD_LEFT);<br />	}<br /><br />	echo "XOR:" . $xor . "\r\n";<br />	echo "REVC_XOR:" . $revc_xor . "\r\n\r\n";<br />
Copy after login

其中$verify_data是一串16进制数,从bin2hex(socket_read($msgsock,8192,PHP_BINARY_READ))传过来的。
谢谢
------解决思路----------------------
$s = '81000000003e900000000000000010001031323334353632';<br />$a = unpack('C*', pack('H*', $s));<br />$x = 29;<br />foreach($a as $v) $x ^= $v;<br />printf('%08b', $x);
Copy after login
00000111

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template