[英]How to convert Unicode Hex Characters to Latin-1 in JavaScript
I'm using rae-api to get the definition of the words in their dictionary.我正在使用rae-api来获取他们字典中单词的定义。 The problem is for example: I search for the definition of the word
hola
, it returns como salutación familiar.
问题是例如:我搜索单词
hola
的定义,它返回como salutación familiar.
. . I want to get the value of
ó
我想得到
ó
的值in Latin-1 characters: ó
, therefore, the result would be como salutación familiar.
在 Latin-1 字符中:
ó
,因此,结果将是como salutación familiar.
getHex
function removes &#;
getHex
function 删除&#;
and returns xF3
to the text.并将
xF3
返回到文本。 However, i want to convert all Unicode Hex characters to Latin-1.但是,我想将所有 Unicode 十六进制字符转换为 Latin-1。
I have tested a lot of answers in similar problems, but none of they works for me (example: decodeURIComponent
or using Hex to utf8 libraries).我已经在类似问题中测试了很多答案,但它们都不适合我(例如:
decodeURIComponent
或使用 Hex 到 utf8 库)。 I'm using Discord.js .我正在使用Discord.js 。
userInput
is the word to search foruserInput
是要搜索的词
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!")})
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.