I’m using rae-api to get the definition of the words in their dictionary. The problem is for example: I search for the definition of the word hola
, it returns como salutación familiar.
. I want to get the value of ó
in Latin-1 characters: ó
, therefore, the result would be como salutación familiar.
getHex
function removes &#;
and returns xF3
to the text. However, i want to convert all Unicode Hex characters to 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). I’m using Discord.js.
userInput
is the word to search for
JavaScript
x
29
29
1
const { RAE } = require("rae-api");
2
const rae = new RAE();
3
4
//----------------- RAE -------------------------
5
6
7
function getHex(text) {
8
text = text.replace(/&#(.*?);/g, function (a, b) {
9
//Get the Latin-1 value of b and return it to the text
10
return b;
11
})
12
13
return text;
14
}
15
16
rae.searchWord(`${userInput}`).then(r => {
17
let wordID = r.getRes()[0].getId();
18
19
rae.fetchWord(wordID).then(word => {
20
let defs = word.getDefinitions();
21
let definition = defs[0].getDefinition();
22
23
return message.channel.send(`La definición de ${userInput} es: ${getHex(definition)}`);
24
})
25
26
}).catch(e => { return message.channel.send("No se encontró esa palabra!")})
27
28
29
Advertisement
Answer
JavaScript
1
6
1
var input = 'F3';
2
var decimalValue = parseInt(input, 16); // Base 16 or hexadecimal
3
var character = String.fromCharCode(decimalValue);
4
console.log('Input:', input);
5
console.log('Decimal value:', decimalValue);
6
console.log('Character representation:', character);