Which line uses the \ x prefix and how to read it

I have a line like this

"\x27\x18\xf6,\x03\x12\x8e\xfa\xec\x11\x0dHL" 

when I put it in the browser console, it automatically becomes something else:

 "\x27\x18\xf6,\x03\x12\x8e\xfa\xec\x11\x0dHL" "'รถ,รบรฌHL" 

if I do chatAt(x) on this line, I get:

 "\x27\x18\xf6,\x03\x12\x8e\xfa\xec\x11\x0dHL".charAt(0) "'" "\x27\x18\xf6,\x03\x12\x8e\xfa\xec\x11\x0dHL".charAt(1) "" "\x27\x18\xf6,\x03\x12\x8e\xfa\xec\x11\x0dHL".charAt(2) "รถ" 

what I want.

Now I want to implement a Java program that reads a string in the same way as in a browser.

The problem is that Java does not recognize the encoding method of this string. Instead, it treats it like a regular string:

 "\\x27\\x18\\xf6,\\x03\\x12\\x8e\\xfa\\xec\\x11\\x0dHL".charAt(0) == '\' "\\x27\\x18\\xf6,\\x03\\x12\\x8e\\xfa\\xec\\x11\\x0dHL".charAt(1) == 'x' "\\x27\\x18\\xf6,\\x03\\x12\\x8e\\xfa\\xec\\x11\\x0dHL".charAt(2) == '2' 

What encoding is encoded by this string? What encoding uses the \x prefix? Is there a way to read it correctly (get the same result as in the browser)?

update: I found a solution -> I think this is not the best, but it works for me:

 StringEscapeUtils.unescapeJava("\\x27\\x18\\xf6,\\x03\\x12\\x8e\\xfa\\xec\\x11\\x0dHL".replace("\\x", "\\u00")) 

Thank you all for your answers :) especially Ricardo Casheri

thanks

+6
source share
2 answers

\x03 is the hexadecimal value of an ASCII char

so: "\x30\x31" matches: "01"

see this page: http://www.asciitable.com

Another thing is when you copy your string without quotes, your IDE will convert any \ to \\

Java String uses unicode escape, so: "\x30\0x31" in java: "\u0030\u0031" ;

you cannot use this escape sequence in Java String \u000a AND \u000d , you must convert it accordingly to \r AND \n

So this "\u0027\u0018\u00f6,\u0003\u0012\u008e\u00fa\u00ec\u0011\rHL" is a Java conversion of this: "\x27\x18\xf6,\x03\x12\x8e\xfa\xec\x11\x0dHL"

+7
source

. To do this, use the apache helper:

 StringEscapeUtils.unescapeJava(...) 

Unescapes any Java literals found in String. For example, it will turn the sequence "\" and "n" into a newline if another "\" precedes "\".

+1
source

Source: https://habr.com/ru/post/945733/


All Articles