Solved

Convert NCR to javascript \u?

Posted on 2009-04-06
2
2,222 Views
Last Modified: 2012-05-06
Hi, I'm trying to convert text in "numerical character reference" format to javascript escape (\u) format.  for example 소개 should become \uC18C\uAC1C.

I need to do this conversion in a Javascript routine or PHP.

Any ideas?  I'm lost
kenw232@yahoo.com
0
Comment
Question by:scottg
  • 2
2 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
If it is a string:



 <script>

 function d2h(d) {return d.toString(16).toUpperCase();}

 function ncr2utf(str) {

   var chars = str.replace(/\&\#/g,'').split(';')

   for (var i=0;i<chars.length;i++) if (chars[i]) chars[i]="\\u"+d2h(parseInt(chars[i]));

   return chars.join(''); 

 }

 document.write(ncr2utf("&#49548;&#44060;")) //  should become \uC18C\uAC1C.

 </script>

Open in new window

0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
Comment Utility
Clearer to see


 <script>

 function d2h(d) {return d.toString(16).toUpperCase();}

 function ncr2utf(str) {

   var chars = str.replace(/\&\#/g,'').split(';')

   for (var i=0;i<chars.length;i++) if (chars[i]) chars[i]="\\u"+d2h(parseInt(chars[i]));

   return chars.join(''); 

 }

 var str = "&#49548;&#44060;"

 document.write(str.replace(/&/g,'&amp;')+':['+str+']:['+ncr2utf(str)+']') //  should become \uC18C\uAC1C.

 </script>

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now