charCodeAt(): getting UTF-16 at given position

By using any char we can get the Unicode number in JavaScript by using charCodeAt().
str='FBA';
document.write(str.charCodeAt(0)); // output 70
In above line the UTF-16 value of first char is returned. ( this is same as Unicode value of single UTF-16 code unit )
Here is an example to generate UTF-16 (same as Unicode ) numbers from a string. We will use string length and for loop to display unicode number of each char of the string.
<script type="text/javascript">
var str='PQRS'; // Sample string

for(var i=0;i<str.length;i++){
document.write(str.charCodeAt(i));
document.write ("<br>");
}
</script>
The Output is here
80
81
82
83

From UTF-16 to Char

By using String.fromCharCode() we can get back the Char.

Here is the code to get string from UTF-16 (Unicode ).
<script>
document.write(String.fromCharCode(80));
document.write(String.fromCharCode(81));
</script>
Output will be
PQ

From UTF-16 to Char by using String.formCharCode

Your Rating




Google+

Post Comment This is for short comments only. Use the forum for more discussions.




HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2018 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer