【问题标题】:Character conversion - fromCharCode problems字符转换——fromCharCode问题
【发布时间】:2018-05-17 20:52:06
【问题描述】:

我有一个与端点通信的 Javascript UI。端点以 JSON 对象数组的形式返回密码短语质询。每个对象都有密码短语中的字符索引和字符代码点值。

[{"index":3,"nValue":101},{"index":5,"nValue":114},{"index":6}]

我正在努力将代码点转换回字母。我收到 RangeError 异常。我不确定为什么我不能将值解析为数字并将其传递给 fromCharCode 方法。

for(var i = 0;i<result.length;i++){
    console.log(typeof result[i].nValue);               //prints string
    console.log('returnVal=*'+result[i].nValue+'*');    //prints *e*
    var intChar = parseInt(result[i].nValue);
    console.log(typeof intChar);                        //prints number
    console.log('intChar=*'+intChar+'*');               //prints *NaN*
    console.log('converted='+String.fromCharCode(result[i].nValue));  //empty string
    console.log('converted='+String.fromCodePoint(result[i].nValue));  //trows range exception
}

【问题讨论】:

    标签: javascript json string unicode type-conversion


    【解决方案1】:

    {"index":6} 没有 nValue 因此返回 undefined

    【讨论】:

      猜你喜欢
      • 2012-06-23
      • 1970-01-01
      • 2017-07-12
      • 2019-07-25
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 2012-12-06
      • 2016-05-26
      相关资源
      最近更新 更多