Solved

Convert NCR to javascript \u?

Posted on 2009-04-06
2
2,245 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
ID: 24084601
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
ID: 24084628
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to hyperlink a document in MS Word w/o full path 1 30
Changing alignment and creating border 6 28
div to fit another div 8 22
Jquery keyup 4 18
This article discusses how to create an extensible mechanism for linked drop downs.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

772 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