There's a website I'm trying to scrape that uses some special encoder for its query string. Special characters are encoded to the "UTF-8 Bytes" values seen in the rightmost column of the table on this site http://www.i18nqa.com/debug/utf8-debug.html. Is there some way I can replicate this on my own in Java, e.g. turn "æ" into "%C3 %A6"?
Asked
Active
Viewed 808 times
0
-
2Try "ae".getBytes("UTF-8"); – Ankur Shanbhag Aug 21 '15 at 18:16
-
And how do I then turn the byte array into the "%C3 %A6"? – Soggiorno Aug 21 '15 at 18:25
1 Answers
-1
Convert from String to byte[]:
String strInput = "Apple";
byte[] utf8ByteOutput = strInput.getBytes("UTF-8");
Convert from byte[] to String:
byte[] utf8ByteInput = {(byte)65, (byte)112, (byte)112, (byte)108, (byte)101};
String strOutput = new String(utf8ByteInput, "UTF-8");

1218985
- 7,531
- 2
- 25
- 31
-
bytes = "æ".getBytes("UTF-8"); System.out.println(new String(bytes, "UTF-8")); Yields "?", not "%C3 %A6"... – Soggiorno Aug 21 '15 at 18:34