18

I'm translating an encryption function from PHP to JS.

PHP: (Both $y and $z are ASCII characters, so $x is inherently an ASCII oddity.)

 $x = ($y ^ $z);

Doing the same in JS results in $x = 0.

I tried:

 $x = String.fromCharCode(($y).charCodeAt(0).toString(2) ^ ($z).charCodeAt(0).toString(2));

But it gets to a different result.

2 Answers 2

32

You don't need to convert it back to a string. Bitwise operators work on numbers. 1 ^ 310 is the same as 1 ^ 112 is the same as 1 ^ 103.

//this should work for single characters.
x = String.fromCharCode(y.charCodeAt(0) ^ z.charCodeAt(0));
Sign up to request clarification or add additional context in comments.

Comments

10

The toString(2) converts to a binary String, but you want to work on the Number type.

Simply drop the toString(2) part and it should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.