0

As I'm trying to convert below hex to java string. As this is UTF8 Characters.

ЫЙБПАРО As this string is converted into hexadecimal 42b41941141f41042041e value.

I tried to convert string to hex by using below javascript code

function encode(string) {
     var str= "";
        var length = string.length;        
        for (var i = 0; i < length; i++){
            str+= string.charCodeAt(i).toString(16);
            }
        return str;
}

I need to decode in java code what would be the way.

Tried below answers

mkyong.com/java/how-to-convert-hex-to-ascii-in-java

stackoverflow.com/questions/140131

Convert a string representation of a hex dump to a byte array using Java?

Community
  • 1
  • 1
Rahul P
  • 1,065
  • 3
  • 17
  • 32
  • 1
    Anybody could ask more information before down voting the question. – Rahul P Jan 22 '15 at 14:01
  • We should not need to ask for more information. You should provide the code you have tried from the start. BTW I did not down vote your question. – Adriaan Koster Jan 22 '15 at 14:03
  • 3
    Your question is *really* vague. It's not clear whether your input is actually the plain text or hex, or what encoding is being used, or what output you're expecting, or what you've already tried. – Jon Skeet Jan 22 '15 at 14:03
  • 1
    Refer link : http://stackoverflow.com/questions/28086916/encode-decode-hex-to-utf-8-string – Rahul P Jan 22 '15 at 14:03
  • 2
    apart from all the above (@JonSkeet hiya there, Jon!) PLZCANIHAZTEHCODEZ questions aren't taken kindly here. –  Jan 22 '15 at 14:04
  • 1
    We shouldn't have to read a *different* question in order to understand *this* one. It's still not clear to me whether you're actually just trying to ask the same question again. – Jon Skeet Jan 22 '15 at 14:05
  • 1
    http://stackoverflow.com/questions/140131 may be relevant though. – Jon Skeet Jan 22 '15 at 14:05
  • Could you please refer the question that i raised which was not answered. For that I'm trying to split my question in asking again. – Rahul P Jan 22 '15 at 14:06
  • 1
    Maybe this example helps you : http://www.mkyong.com/java/how-to-convert-hex-to-ascii-in-java/ – JHDev Jan 22 '15 at 14:06
  • @RahulP just because your previous question wasn't answered in a way satisfactory to you doesn't make this one a proper, understandable, unambiguous and spot-on question. –  Jan 22 '15 at 14:07
  • 1
    right but somebody could ask me to provide details instead of down vote it. – Rahul P Jan 22 '15 at 14:09
  • Your hex String is missing at least one digit. The number must be even. – SubOptimal Jan 22 '15 at 14:13
  • How do you code the String to hexadecimal – JHDev Jan 22 '15 at 14:17
  • esprittn : Added javascript snippet which converts string to hex which I'm sending through URL parameter – Rahul P Jan 22 '15 at 14:32
  • 1
    I find a solution: if you can change your JS function to generate a hex like this \u042B\u0419\u0411\u041F\u0410\u0420\u041E( hex code for ЫЙБПАРО) you can decode easily. I get this Hex code from this converter http://itpro.cz/juniconv/ I put it on System.out.println and I get ЫЙБПАРО and you must use UTF-8 on your project. – JHDev Jan 22 '15 at 16:20

2 Answers2

2

You can try Apache Codec.

byte[] b= Hex.decodeHex(yourhexdecimalstring.toCharArray());
System.out.println(new String(b, "UTF8"));
Jeremy
  • 776
  • 1
  • 7
  • 18
1

Let's start with the first character: this is #1067 in the Unicode table. This means 4 x 256 + 43. So the hex-representation of this is 04 and 2B (or vice versa depending on the byte order). Yours seems to be 42 b4. So I assume the encoding is not correct.
I'm not familiar with jscript but this code...

private void jscript() throws ScriptException {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval("function encode(string) {\n"
            + "     var str= \"\";\n"
            + "        var length = string.length;        \n"
            + "        for (var i = 0; i < length; i++){\n"
            + "            str+= string.charCodeAt(i).toString(16);\n"
            + "            }\n"
            + "        return str;\n"
            + "}"
            + ""
            + "print(encode(\"ЫЙБПАРО\"));");
}

...returns this

42b41941141f41042041e

as you wrote. But in my opinion this is not correct

chris
  • 1,685
  • 3
  • 18
  • 28