utf 8 - Decode Unicode to character in javascript - Stack Overflow

I have the following unicode sequence:d76cb9dd0020b370b2c8c758I tried randomly in non-English character

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?

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?

Share Improve this question asked Jan 21, 2013 at 23:46 Doni Andri CahyonoDoni Andri Cahyono 7935 gold badges16 silver badges29 bronze badges 2
  • 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 Commented Jan 21, 2013 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 Commented Jan 23, 2013 at 6:42
Add a ment  | 

2 Answers 2

Reset to default 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.

How about

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

DEMO

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742345188a4426403.html

相关推荐

  • utf 8 - Decode Unicode to character in javascript - Stack Overflow

    I have the following unicode sequence:d76cb9dd0020b370b2c8c758I tried randomly in non-English character

    9小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信