# Decimal to hex?

simple question
how do i convert from decimal to hex?

Thank you

http://forums.electricimp.com/discussion/623/squirrel-hex-to-integer/p1

Whichever desktop OS you use, you’ll find the Calculator utility - on Mac OS X, Windows and Ubuntu/Mint Linux - has a Programmer mode. Here you can enter hex, decimal and - IMHO, more useful - binary numbers and convert between them.

That’s the modern way. I prefer the retro way: I have a Casio calculator I keep handy just for this kind of thing.

Oh, I thought he meant converting using Squirrel.

There are many online converts available also.

I came up with this neat function the other day.
It will take any length of hex string and return an integer.
You will have to strip off the 0x before calling the function.

`// Parses a hex string and turns it into an integer
function hextoint(str) {

``````local hex = 0x0000;
foreach (ch in str) {
local nibble;
if (ch >= '0' && ch <= '9') {
nibble = (ch - '0');
} else {
nibble = (ch - 'A' + 10);
}
hex = (hex << 4) + nibble;
}
return hex;
``````

}
`

why not javascript instead of squirrel?
Its event based also

@ramstein74 - Squirrel is a very compact language compared to Javascript, which is very important for embedded devices.

Thanks aron, that’s useful. I have similar functions for manipulating hex strings. The only thing I note with yours is that it expects all uppercase values. You could try the following:
`// convert ascii nibble to 0-15; local nibble = ch - '0'; if (nibble > 9) nibble = ((nibble & 0x1f) - 7);`

Hey coverdriven,

I just had to thank-you for this snippet. I’ve found it so useful! And so compact too.