简体   繁体   English

两个字符串的异或 Angular

[英]XOR of two strings Angular

I have to perform a XOR operation on the two strings, I got the python implementation of it:我必须对这两个字符串执行异或运算,我得到了它的 python 实现:

def sxor(s1,s2):    
    return ''.join(chr(ord(a) ^ ord(b)) for a,b in zip(s1,s2)) 

In the above code, we are running the for loop on the strings and we are creating the tuples of it.在上面的代码中,我们在字符串上运行 for 循环并创建它的元组。

string 1: abcdefghijklmn

string 2: 12345

it will create the tuples out of it它将从中创建元组

[('a','1'),('b','2'),('c','3'),('d','4'),('e','5')]

we have to perform the xor operation on this tuples我们必须对这个元组执行异或操作

chr(ord('a')^ord('1'))  which will result => p    
chr(ord('b')^ord('2'))  which will result => p    
chr(ord('c')^ord('3'))  which will result => p    
chr(ord('d')^ord('4'))  which will result => p    
chr(ord('e')^ord('5'))  which will result => p

I want output as ppppp .我想要 output 作为ppppp I want to implement the same algorithm in angular我想在 angular 中实现相同的算法

I have done this我做了这个

export class AppComponent implements OnInit {
  name = 'Angular ' + VERSION.major;
  array1 = [1, 7, 8, 8, 7];
  array2 = ['r', 'u', 'm', 'a', 'n'];
  zip: any;
  ordValue: any;
  value: any;

  ngOnInit(): void {
    this.ordValue = this.ord('r') ^ this.ord('1');
    console.log(this.ordValue);
    this.value = String.fromCharCode(this.ordValue);
    console.log('ASCII ' + this.value);
  }

  ord(str) {
    return str.charCodeAt(0);
  }
}

which is giving the character after XOR在 XOR 之后给出字符

stackblitz Demo: stackblitz stackblitz 演示: stackblitz

You could use the String.fromCodePoint() and the String.charCodeAt() functions.您可以使用 String.fromCodePoint() 和 String.charCodeAt() 函数。 Something like this (I didn't test it):像这样的(我没有测试):

function sxor (s1, s2){
let str = '';
for (let i = 0; i < Math.min(s1.length, s2.length); ++i)
    str += String.fromCodePoint(s1.charCodeAt(i) ^ s2.charCodeAt(i));
return str;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM