1

I have this below String, I don't know it is in which format honestly?

Under%20\302\243100%2E00

And I need to convert the above String in UTF-8 format and the output should be like below for above String.

Under%20£100%2E00

So Basically How can I convert any String to UTF-8 format in Java?

AKIWEB
  • 19,008
  • 67
  • 180
  • 294
  • 3
    That's UTF-8 data partially URL-encoded and then decoded as Latin-1. It results in "Under £100.00" when fully decoded. – Ignacio Vazquez-Abrams Aug 11 '12 at 01:49
  • 1
    I don't understand why you need to encode the decimal point (`'.'`) as `"%2E"` in the final string. It is not necessary if this string is intended to be part of a URL, HTML, XML, JSON ... or anything else that immediately springs to mind. – Stephen C Aug 11 '12 at 03:26

1 Answers1

2

What you have in that character sequence (which is not a Java string literal) is a mixture of two encodings: octal encoding and url encoding. Octal encoding looks like \xxx where xxx is a character code in octal. Url encoding looks like %XX where XX is a character code in hex. Octal encoding is actually part of the Java specification. Now, converting octal into a Java string isn't trivial. There is no built-in method for that. Look at this thread, though, for ideas on how to do it: How to unescape a Java string literal in Java?.

Community
  • 1
  • 1
Dmitry B.
  • 9,107
  • 3
  • 43
  • 64
  • It should be pointed out that this is not a Java string literal ... so the "ideas" in the linked article do not carry over automatically. – Stephen C Aug 11 '12 at 03:29
  • you're right, I should make it more explicit that the above is not a string literal. – Dmitry B. Aug 11 '12 at 15:10
  • So is there any way I can convert the above String in UTF-8 format? Or some Java functions? – AKIWEB Aug 11 '12 at 23:29