I am trying to convert a number into hexadecimal using the number.toString(16) function but it is giving me the error Error Expected 0 arguments, but got 1.
.
I might be doing something wrong with my types, because I believe it should work.
Here is my code :
type ImageToEncode = {
pixelsAndColors: string[][][];
bounds: string[][][];
paletteIndex: string;
}
task("encodeImage", "Encode an image", async function(taskArgs: ImageToEncode) {
const hexPixelsAndColors = taskArgs.pixelsAndColors
.map((array: string[][]) => {
let firstChar = array[0].toString(16);
let secondChar = array[1].toString(16);
if(firstChar.length < 2) {
firstChar = `0${firstChar}`;
}
if(secondChar.length < 2) {
secondChar =`0${secondChar}`;
}
return [firstChar, secondChar];
})
.map((array: string[]) => array.join(""))
.join("");
const hexBounds = taskArgs.bounds.map(bound => {
let firstChar = bound.toString(16);
if(firstChar.length < 2) {
firstChar = `0${firstChar}`;
}
return firstChar;
}).join("");
const hexData = `0x${taskArgs.paletteIndex}${hexBounds}${hexPixelsAndColors}`
console.log(hexData);
return hexData;
});
For information can be interpreted like a function.
CodePudding user response:
toString
expects an argument for number.toString
whereas you are trying to call toString
with array of strings, or even string[][]
. It is not correct let firstChar = array[0].toString(16) // expected error
.
If you want to convert string
to hexadecimal
you should use parseInt(string, 10).toString(16)
.
For example: parseInt("100", 10).toString(16) // 64
If you have an array of strings, you can parse the whole array with help of this function:
const toHex = (str: string) => parseInt(str, 10).toString(16)
const result = ['10', '42'].map(toHex)