0

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"?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Soggiorno
  • 760
  • 9
  • 17

1 Answers1

-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