2

I have string in the following format:

String input = "4E 65 73 74 6C C3 A9"

Which I need to convert to below output:

Nestlé

Is there any utility library or java function I can use to make it work?

3
  • You can create a byte[] by parsing the hexi-decimal numbers and then UTF-8 decode the result. Commented Jun 5, 2014 at 3:31
  • You've mis-stated your problem. What you have is a string containing space-separated hex digits representing UTF-8 code points. There's enough information in that description for you to be able to solve your problem completely. Commented Jun 5, 2014 at 3:34
  • @EJP can you give share code examples Commented Jun 5, 2014 at 3:37

2 Answers 2

3

try javax.xml.bind.DatatypeConverter

    String input = "4E 65 73 74 6C C3 A9";
    byte[] a = DatatypeConverter.parseHexBinary(input.replace(" ", ""));
    String output = new String(a, encoding); <-- you need to know the input encoding
Sign up to request clarification or add additional context in comments.

Comments

2

Try,

String input = "4E 65 73 74 6C C3 A9";
String[] hex=input.split(" ");

for(String h:hex){
   int value=Integer.parseInt(h,16);
   System.out.print((char)value);
}

2 Comments

+1 @dhaval That's the correct answer, see the extended ASCII table: ascii-code.com
@alfasin yes it seems correct but I needed the short and direct solution and Evgeniy's solution works for me now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.