Unicode 值 uXXXX 到 Javascript 中的字符 [英] Unicode value uXXXX to Character in Javascript

查看:21
本文介绍了Unicode 值 uXXXX 到 Javascript 中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未这样做过,不知道为什么它会输出臭名昭著的 编码字符.关于如何输出字符的任何想法(ASCII + Unicode)?我认为 u0041-u005A 应该在 UTF-8 中打印 A-Z,Firefox 报告的是页面编码.

I've never done this before and am not sure why it's outputting the infamous encoding character. Any ideas on how to output characters as they should (ASCII+Unicode)? I think u0041-u005A should print A-Z in UTF-8, which Firefox is reporting is the page encoding.

   var c   = new Array("F","E","D","C","B","A",9,8,7,6,5,4,3,2,1,0);
   var n   = 0;
   var d   = "";
   var o   = "";

   for (var i=16;i--;){
      for (var j=16;j--;){
         for (var k=16;k--;){
            for (var l=16;l--;){

               d =  c[i].toString()
                 +  c[j].toString()
                 +  c[k].toString()
                 +  c[l].toString();

               o += ( ++n + ": " 
                    + d   + " = " 
                    + String.fromCharCode("\u" + d) 
                    + "
<br />" );

               if(n>=500){i=j=k=l=0;} // stop early
            }
         }
      }
   }

   document.write(o);

推荐答案

.fromCharCode() 函数接受一个数字,而不是一个字符串.你不能把这样的字符串放在一起,并期望解析器做你认为它会做的事情;这不是语言的工作方式.

The .fromCharCode() function takes a number, not a string. You can't put together a string like that and expect the parser to do what you think it'll do; that's just not the way the language works.

您可以修改您的代码以根据您的十六进制数生成一个字符串(不带 'u'),然后调用

You could ammend your code to make a string (without the 'u') from your hex number, and call

var n = parseInt(hexString, 16);

获取值.然后你可以用那个值调用.fromCharCode().

to get the value. Then you could call .fromCharCode() with that value.

这篇关于Unicode 值 uXXXX 到 Javascript 中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆