1

I have the following hexadecimal string:

const data = '\x03\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x05\x00\x00\x00\x02\x00\x00\x00\x90\x00\x00\x00\x00\x00\x00\x00\x80\x04\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00{\x14\xaeG\xe1zt?\xe9\x86/\xb25\x0e&@\b\x00\x00\x00d\x00\x00\x00\x00\x00\x00\x00\x00'

No matter what I do, the printed output is {.Gazt?i/25& d with a lot of spaces. I really some help to keep it as is, can anyone help me out please?

  • You need to mask the backslashes: `\\x03\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x05\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x90\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x80\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x00\\x00\\x00\\x00{\\x14\\xaeG\\xe1zt?\\xe9\\x86/\\xb25\\x0e&@\\b\\x00\\x00\\x00d\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00` –  Jan 17 '21 at 23:17
  • Didn't steal your answer btw. Just was responding from a smartphone and took a few minutes to enter all the backslashes – DonCarleone Jan 17 '21 at 23:21
  • Does this answer your question? [Is there an \_\_repr\_\_ equivalent for javascript?](https://stackoverflow.com/questions/24902061/is-there-an-repr-equivalent-for-javascript) – JosefZ Jan 17 '21 at 23:34
  • Not sure the link helps, but thanks – Dani Tseitlin Jan 17 '21 at 23:41

2 Answers2

1

To keep it as it is, use double backslashes.

like de code below:

const data = '\\x03\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x05\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x90\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x80\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x00\\x00\\x00\\x00{\\x14\\xaeG\\xe1zt?\\xe9\\x86/\\xb25\\x0e&@\\b\\x00\\x00\\x00d\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'

When you use just one backslash Javascript understand you want the ASCII representation of the Hex number you're passing to them that's why you have a bunch of whitespaces because \x00 is a space, or nothing at all.

Brian Ito
  • 23
  • 6
  • Okay, but what if I get this from an async function? how can I force the response to contain to mask? is there package/function to do this? – Dani Tseitlin Jan 17 '21 at 23:29
  • Yeah, sure. You can do this doing something like: string.replace('/', '//'); But I will try this first. – Brian Ito Jan 17 '21 at 23:37
  • That doesn't work... I did the following: ``` const data = "\x03\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x05\x00\x00\x00\x02\x00\x00\x00\x90\x00\x00\x00\x00\x00\x00\x00\x80\x04\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00{\x14\xaeG\xe1zt?\xe9\x86/\xb25\x0e&@\b\x00\x00\x00d\x00\x00\x00\x00\x00\x00\x00\x00"; const replaced = data.replace(/\//g, "\\"); console.log(replaced) ``` The output is still {®Gázt?é\²5& d – Dani Tseitlin Jan 17 '21 at 23:38
  • Any solution? I am still stuck here – Dani Tseitlin Jan 18 '21 at 20:30
0

Just escape the backslashes. 'x03\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x05\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x90\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x80\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x00\\x00\\x00\\x00{\\x14\\xaeG\\xe1zt?\\xe9\\x86\/\\xb25\\x0e&@\\b\\x00\\x00\\x00d\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'

DonCarleone
  • 544
  • 11
  • 20