0

I have the following unicode sequence:

          d76cb9dd0020b370b2c8c758

I tried randomly in non-English character (for this experiment, I tried korean languange) as the original of above unicode lines :

          희망 데니의

How can i decode those-above-mentioned unicode sequence into the original form?

Doni Andri Cahyono
  • 793
  • 5
  • 16
  • 28
  • 1
    What is the sequence above? It is not valid hex-encoded UTF-8. It looks like UTF-16BE (where did you get it?), though. – John Dvorak Jan 21 '13 at 23:50
  • @JanDvorak: I think so since I got it from remote office at Head Office. I just use and try to figure out how to decode it which fortunately both Musa and gilly3 already gave the correct answer. – Doni Andri Cahyono Jan 23 '13 at 06:42

2 Answers2

4

As a JavaScript string literal, escape hex codes with \u:

var koreanString = "\ud76c\ub9dd\u0020\ub370\ub2c8\uc758";

Or just enter the korean characters into the string:

var koreanString = "희망 데니의";

To process a hex string representing unicode characters, parse the hex string to numbers and the build the unicode string use String.fromCharCode():

var hex = "d76cb9dd0020b370b2c8c758";
var koreanString = "";
for (var i = 0; i < hex.length; i += 4) {
    koreanString += String.fromCharCode(parseInt(hex.substring(i, 4), 16));
}

Edit: You can get the length of any string by accessing its length property:

var stringLength = koreanString.length;

This will return 6. There is no "english" string. You have a string representing hexadecimal numbers, and hexadecimal numbers consist of characters from the latin character set, but these are not in any spoken language. They are just numbers. You can, of course, get the length of the hexadecimal string using the length property, but I'm not sure why you'd want to do that. It would be more straight forward to use an array of numbers instead of a string:

var charCodes = [0xd76c, 0xb9dd, 0x0020, 0xb370, 0xb2c8, 0xc758];
var koreanString = String.fromCharCode.apply(null, charCodes);

In this way, charCodes.length will be the same as koreanString.length.

gilly3
  • 87,962
  • 25
  • 144
  • 176
  • thanks a lot, both Musa and you give the correct answer. Btw, how can i know the length of koreanString? Is it from 'i'? – Doni Andri Cahyono Jan 22 '13 at 02:35
  • @DoniAndriCahyono - I edited my answer with some comments regarding accessing string length. – gilly3 Jan 22 '13 at 18:44
  • I will use it to determine the length of character to be used later on via SMS/text since I'm developing web-based SMS and it must comply with international characters. Thanks anyway. – Doni Andri Cahyono Jan 23 '13 at 06:44
3

How about

var str = 'd76cb9dd0020b370b2c8c758';
str = '"'+str.replace(/([0-9a-z]{4})/g, '\\u$1')+'"';
alert(JSON.parse(str));

DEMO

Musa
  • 96,336
  • 17
  • 118
  • 137
  • Thanks. Both answer of you and gilly3 are what i am looking for. Btw, how can i know the length of its str? – Doni Andri Cahyono Jan 22 '13 at 02:36
  • @DoniAndriCahyono just use the `length` property of the string. see http://jsfiddle.net/mowglisanu/Y9uea/1/ – Musa Jan 22 '13 at 04:30
  • I meant, the length of unicode character is different from english character, right? So, how can i determine this? Is it using ***length*** property as you've already given? – Doni Andri Cahyono Jan 22 '13 at 06:17