0
> str = '\xae\xee'
'®î'

How to convert [0xae, 0xee] to '®î'?

3
  • hope this link help for you gist.github.com/tauzen/3d18825ae41ff3fc8981 Commented Dec 2, 2019 at 7:32
  • sorry, not tested String.fromCharCode, I thought it can only handle numbers less than 128 Commented Dec 2, 2019 at 7:33
  • actually I tested String.fromCharCode(128), it is ' ', gave me that wrong expectation Commented Dec 2, 2019 at 7:39

1 Answer 1

3

You can use String.fromCharCode() to convert the hex values to their string representation with .map() and .join() to form a string:

const hex = [0xae, 0xee];
const res = hex.map(s => String.fromCharCode(s)).join('');
console.log(res);

Sign up to request clarification or add additional context in comments.

6 Comments

@georg ah nice, very clean
strange, '®\u0000\u0000î\u0001\u0000'
@thinker with what input?
> [0xae, 0xee].map(String.fromCharCode).join(''); '®\u0000\u0000î\u0001\u0000'
@thinker3 the reason why .map() isn't working here is because .map() will pass through the index as the second argument for String.fromCharCode(). We need to explicitly call it (.map(x => String.fromCharCode(x))) - I've updated my code
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.