Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of string uses prefix \x and how to read it

I have a string like this

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

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

"\x27\x18\xf6,\x03\x12\x8e\xfa\xec\x11\x0dHL"
"'ö,úìHL"

if I do chatAt(x) over this string, 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)
"ö"

which IS what I want.

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

The problem is, Java does not recognize the way this string is encoded. Instead, it treats it as a normal 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 kind of encoding this string is encoded? What kind of encoding uses prefix \x? Is there a way to read it properly (get the same result as in browser)?

update: I found a solution -> i guess it 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 replies :) especially Ricardo Cacheira

Thank you

like image 205
user2375809 Avatar asked May 23 '13 22:05

user2375809


People also ask

How do you find the prefix of a string?

A prefix of a string S is any leading contiguous part of S. A suffix of the string S is any trailing contiguous part of S. For example, "c" and "cod" are prefixes, and "ty" and "ity" are suffixes of the string "codility".


2 Answers

\x03 is the ASCII hexadecimal value of char

so this: "\x30\x31" is the same as : "01"

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

Another thing is when you copy your string without quotation marks your IDE converts any \ to \\

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

you can't use these escape sequence in Java String \u000a AND \u000d you should convert it respectively to \r AND \n

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

like image 113
Ricardo Cacheira Avatar answered Sep 19 '22 21:09

Ricardo Cacheira


apache commons provides a helper for this:

StringEscapeUtils.unescapeJava(...)

Unescapes any Java literals found in the String. For example, it will turn a sequence of '\' and 'n' into a newline character, unless the '\' is preceded by another '\'.

like image 39
Steve Oh Avatar answered Sep 22 '22 21:09

Steve Oh