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

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

问题描述

我从来没有这样做,我不知道为什么它输出臭名昭着的 编码字符。任何想法如何输出字符,因为他们应该(ASCII + Unicode)?我认为 \\\A - \\\Z 应打印 AZ

  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 函数接受一个数字,而不是字符串。你不能把一个这样的字符串放在一起,期望解析器做你认为它会做;



您可以改写您的代码,以从十六进制数字中创建一个字符串(不带\u),然后调用

  var n = parseInt(hexString,16); 

获取值。然后您可以使用 值调用 .fromCharCode()


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) 
                    + "\n<br />" );

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

   document.write(o);

解决方案

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.

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

var n = parseInt(hexString, 16);

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

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

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