How to decode to UTF-8 String from Hex encoded string

Jerry Cai

I get the HTML Javascript string such as :

htmlString = "https\x3a\x2f\x2ftest.com"

But I want to decode it as below :

str = "https://test.com"

That means , I want a Util API like :

 public static String decodeHex(String htmlString){
   // do decode and converter here 
 }

 public static void main(String ...args){
       String htmlString = "https\x3a\x2f\x2ftest.com";
       String str = decodeHex(htmlString);
       // str should be "https://test.com"
 }

Does anybody know how to implement this API - decodeHex ?

Aurand

This should be enough to get you started. I leave implementing hexDecode and sorting out malformed input as an exercise for you.

public String decode(String encoded) {
  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < encoded.length(); i++) {
    if (encoded.charAt(i) == '\' && (i + 3) < encoded.length() && encoded.charAt(i + 1) == 'x') {
      sb.append(hexDecode(encoded.substring(i + 2, i + 4)));
      i += 3;
    } else {
      sb.append(encoded.charAt(i));
    }
  }
  return sb.toString;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to decode an utf8 encoded string split in two buffers right in between a 4 byte long char?

From Dev

How to decode a string UTF-8 in ruby?

From Dev

How to convert a string encoded in utf16 to a string encoded in UTF-8?

From Dev

Getting a utf-8 encoded string from a database, then displaying in a webview

From Dev

How do you convert a base64 utf-8 encoded string to a binary file from bash?

From Dev

How to read a UTF-8 encoded list of string tokens into a vector?

From Dev

How to get a file list as utf8 encoded string into gnuplot?

From Dev

How to decode UTF-8 string returned from shell_exec()?

From Dev

Decode Class from @encoded type string

From Dev

std::string and UTF-8 encoded unicode

From Dev

How to decode string representative of utf-8 with python?

From Dev

How should I decode a UTF-8 string

From Dev

How to decode string in specific charset and then encode it in UTF-8?

From Dev

Getting UTF-8 encoded from US-ASCII encoded string

From Dev

Getting UTF-8 encoded from US-ASCII encoded string

From Dev

How to decode this seemingly GBK-encoded string?

From Dev

How to decode the URL with encoded Query String

From Dev

How to convert large UTF-8 encoded char* string to CStringW (UTF-16)?

From Dev

Converting url encoded string(utf-8) to string in python?

From Dev

comparing a url containing utf-8 encoded string with a string

From Dev

how to decode a string(not bytes[]) in utf-8 format into another string in java?

From Dev

Using php preg_replace to remove some punctuations from utf-8 encoded string

From Dev

Decode an ENCODED unicode string in Python

From Dev

Decode an ENCODED unicode string in Python

From Dev

Decode multiple times encoded String

From Dev

Decode file to stream, from base64 encoded string. How to do that?

From Dev

How do I get hex blocks from a base 64 encoded string?

From Dev

How can I know if url-encoded string is UTF-8 or Latin-1 with PHP?

From Dev

Byte array is a valid UTF8 encoded String in Java but not in Python

Related Related

  1. 1

    How to decode an utf8 encoded string split in two buffers right in between a 4 byte long char?

  2. 2

    How to decode a string UTF-8 in ruby?

  3. 3

    How to convert a string encoded in utf16 to a string encoded in UTF-8?

  4. 4

    Getting a utf-8 encoded string from a database, then displaying in a webview

  5. 5

    How do you convert a base64 utf-8 encoded string to a binary file from bash?

  6. 6

    How to read a UTF-8 encoded list of string tokens into a vector?

  7. 7

    How to get a file list as utf8 encoded string into gnuplot?

  8. 8

    How to decode UTF-8 string returned from shell_exec()?

  9. 9

    Decode Class from @encoded type string

  10. 10

    std::string and UTF-8 encoded unicode

  11. 11

    How to decode string representative of utf-8 with python?

  12. 12

    How should I decode a UTF-8 string

  13. 13

    How to decode string in specific charset and then encode it in UTF-8?

  14. 14

    Getting UTF-8 encoded from US-ASCII encoded string

  15. 15

    Getting UTF-8 encoded from US-ASCII encoded string

  16. 16

    How to decode this seemingly GBK-encoded string?

  17. 17

    How to decode the URL with encoded Query String

  18. 18

    How to convert large UTF-8 encoded char* string to CStringW (UTF-16)?

  19. 19

    Converting url encoded string(utf-8) to string in python?

  20. 20

    comparing a url containing utf-8 encoded string with a string

  21. 21

    how to decode a string(not bytes[]) in utf-8 format into another string in java?

  22. 22

    Using php preg_replace to remove some punctuations from utf-8 encoded string

  23. 23

    Decode an ENCODED unicode string in Python

  24. 24

    Decode an ENCODED unicode string in Python

  25. 25

    Decode multiple times encoded String

  26. 26

    Decode file to stream, from base64 encoded string. How to do that?

  27. 27

    How do I get hex blocks from a base 64 encoded string?

  28. 28

    How can I know if url-encoded string is UTF-8 or Latin-1 with PHP?

  29. 29

    Byte array is a valid UTF8 encoded String in Java but not in Python

HotTag

Archive