本文介绍了JavaScript中两个十六进制字符串的XOR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var hex1 = "B1C85C061C98E713DEF0E2EDDDDB432738674C9F8962F09B75E943D55F9FB39F";
var hex2 = "121B0D3327A21B8048FC7CA6FD07AACC0D8DF59B99DB098686696573E3686E6C";
var result = hex1 ^ hex2; //XOR the values
console.log(result); // outputs: 0 which does not sound good.
有关如何对十六进制值执行XOR运算的任何想法?
Any ideas how to perform XOR operations on hex values?
推荐答案
JavaScript中的按位运算仅适用于数值。
Bitwise operations in JavaScript only work on numeric values.
您应该 parseInt(hexString,16)
之前的十六进制字符串。特别是在你的情况下,这不起作用,因为你的十六进制对于一个数字来说太大了。您必须创建自己的自定义XOR函数。
You should parseInt(hexString, 16)
your hex string before. Specifically in your case this wouldn't work because your hex is too big for a number. You would have to create your own customized XOR function.
看一下这个链接:
对于手动XOR,生成的bytearray将易于识别。按字节字节。也许这会有所帮助:。
The resulting bytearray will be ellegible for a manual XOR. Byte by byte. Maybe this will help: Java XOR over two arrays.
这篇关于JavaScript中两个十六进制字符串的XOR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!