Using Google Sheets, I want to store the value in a cell as a hexadecimal number.
But using Utilities.formatString("0x%2x", byte) stores the literal "0x%2x" in the cell.
Using Utilities.formatString("0x%2i", byte) stores "0x [number]" as expected.
Any idea what I am doing wrong?
Best regards Stefan
Here is a function that may do what you want:
// function to convert decimal to hexadecimal
function DecToHex(value) {
var result = "";
while( value != 0 ) {
var temp = value % 16;
Logger.log(temp);
var hex = temp < 10 ? String.fromCharCode(temp+48) : String.fromCharCode(temp+55);
result = hex.concat(result);
value = Math.floor(value/16);
}
if( ( result.length %2 ) != 0 ) result = "0"+result;
result = "0x"+result;
return result;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With