【问题标题】:How to convert Unicode Hex Characters to Latin-1 in JavaScript如何在 JavaScript 中将 Unicode 十六进制字符转换为 Latin-1
【发布时间】:2021-01-24 14:54:18
【问题描述】:

我正在使用 rae-api 来获取他们字典中单词的定义。问题例如:我搜索单词hola的定义,它返回como salutación familiar.。我想获取 ó 的 Latin-1 字符的值:ó,因此,结果将是 como salutación familiar.

getHex 函数删除 &#; 并将 xF3 返回给文本。但是,我想将所有 Unicode 十六进制字符转换为 Latin-1。

我已经在类似问题中测试了很多答案,但它们都不适合我(例如:decodeURIComponent 或使用 Hex 到 utf8 库)。我正在使用 Discord.js

userInput 是要搜索的词

const { RAE } = require("rae-api");
const rae = new RAE();
        
//-----------------  RAE  -------------------------

    
function getHex(text) {
   text = text.replace(/&#(.*?);/g, function (a, b) {
        //Get the Latin-1 value of b and return it to the text
        return b;
   })

   return text;
}

rae.searchWord(`${userInput}`).then(r => {
    let wordID = r.getRes()[0].getId();

rae.fetchWord(wordID).then(word => {
    let defs = word.getDefinitions();
    let definition = defs[0].getDefinition();
        
    return message.channel.send(`La definición de ${userInput} es: ${getHex(definition)}`);
})
    
}).catch(e => { return message.channel.send("No se encontró esa palabra!")})
    

【问题讨论】:

    标签: javascript hex discord.js


    【解决方案1】:

    var input = 'F3';
    var decimalValue = parseInt(input, 16); // Base 16 or hexadecimal
    var character = String.fromCharCode(decimalValue);
    console.log('Input:', input);
    console.log('Decimal value:', decimalValue);
    console.log('Character representation:', character);

    【讨论】:

      猜你喜欢
      • 2013-10-09
      • 2014-12-22
      • 2020-05-19
      • 2021-04-30
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 2018-10-05
      • 2014-09-28
      相关资源
      最近更新 更多