JavaScript 中两个十六进制字符串的异或

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20566421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:47:31  来源:igfitidea点击:

XOR of two hex strings in JavaScript

javascript

提问by thedethfox

var hex1 = "B1C85C061C98E713DEF0E2EDDDDB432738674C9F8962F09B75E943D55F9FB39F";
var hex2 = "121B0D3327A21B8048FC7CA6FD07AACC0D8DF59B99DB098686696573E3686E6C";

var result = hex1 ^ hex2; //XOR the values

console.log(result); // outputs: 0 which does not sound good.

Any ideas how to perform XOR operations on hex values?

任何想法如何对十六进制值执行异或运算?

采纳答案by André Pena

Bitwise operations in JavaScript only work on numeric values.

JavaScript 中的按位运算仅适用于数值。

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.

你应该parseInt(hexString, 16)在你的十六进制字符串之前。特别是在您的情况下,这不起作用,因为您的十六进制对于数字来说太大了。您必须创建自己的自定义 XOR 函数。

Take a look at this link: How to convert hex string into a bytes array, and a bytes array in the hex string?

看看这个链接:How to convert hex string into a bytes array, and a bytes array in the hex string?

The resulting bytearray will be ellegible for a manual XOR. Byte by byte. Maybe this will help: Java XOR over two arrays.

生成的字节数组对于手动 XOR 将是易读的。一个字节一个字节。也许这会有所帮助:Java XOR over two arrays

回答by ajaykools

str = 'abc';
c = '';
key = 'K';
for(i=0; i<str.length; i++) {
    c += String.fromCharCode(str[i].charCodeAt(0).toString(10) ^ key.charCodeAt(0).toString(10)); // XORing with letter 'K'
}
return c;

Output of string 'abc':

字符串“ abc”的输出:

"*)("

回答by user5312217

You can use a function like this.

您可以使用这样的功能。

function xor(a, b) {
  if (!Buffer.isBuffer(a)) a = new Buffer(a)
  if (!Buffer.isBuffer(b)) b = new Buffer(b)
  var res = []
  if (a.length > b.length) {
    for (var i = 0; i < b.length; i++) {
       res.push(a[i] ^ b[i])
    }
 } else {
 for (var i = 0; i < a.length; i++) {
   res.push(a[i] ^ b[i])
   }
 }
 return new Buffer(res);
}

Source: https://github.com/czzarr/node-bitwise-xor

来源:https: //github.com/czzarr/node-bitwise-xor

回答by varun kalra

Below is function that takes in 2 strings like "041234FFFFFFFFFF" and "0000000709000003" (a classic example of pin block and card block) Expected result from the above 2 strings is "041234F8F6FFFFFC"

下面是接受 2 个字符串的函数,如“041234FFFFFFFFFF”和“0000000709000003”(引脚块和卡块的经典示例)上述 2 个字符串的预期结果是“041234F8F6FFFFFC”

function bitwiseXorHexString(pinBlock1, pinBlock2) {
  var result = ''
  for (let index = 0; index < 16; index++) {
    const temp = (parseInt(pinBlock1.charAt(index), 16) ^ parseInt(pinBlock2.charAt(index), 16)).toString(16).toUpperCase()
    result += temp
  }
  return result
}

Note: This was made to xor 2 strings of fixed length 16. You may modify it as per your needs.

注意:这是对 2 个固定长度为 16 的字符串进行的异或。您可以根据需要对其进行修改。