# Hex to Integer

So I get that we can convert numbers to hexadecimals using %Hex2%. But what if we want to convert hex to integers? Can anyone help? Dealing with midi packets here.

Perhaps 'decimal' is a better search term than 'integer' ?

https://wiki.keyboardmaestro.com/token/Calculate

(they are all, of course, integers, whether binary, octal, decimal or hex)

1 Like

I tried that. Can't seem to get the expected result though.

Attached is a screenshot where keyboard maestro is receiving middle C ie note number 60 / hex 3C. Tried converting the tokens but the results are just 0's.

Tried binary, octals, currency, even percentage, can't convert that 3C to 60. My use of syntax are correct in the example right?

How about just executing a script:

Hex to Decimal.kmmacros (2.7 KB)

1 Like

Got it – those are showing values in various bases, rather than reading them.

For reading a Hex string KMVar to a numeric value, I would personally use JS parseInt(String, 16)

1 Like

Or, if possible, use the `zero x` prefix:

1 Like

As @ComplexPoint says, you can use 0xAF to interpret numbers as hex.

Further, you can use the CALCULATE function to interpolate non-numeric variables, so for example:

• Set variable HexVar to text “AF”
• Set variable ValueVar to calculation “CALCULATE(0x%Variable%HexVar%)”

I should add a HEX function for this in the future.

2 Likes

Wow! Thanks complexpoint and mrpasini. Both your script suggestions worked really well!

I never had to deal with scripts before within the keyboard maestro environment. So the use of these approaches is a totally new concept to me.

I'll have to dig deeper into scripting. It seems to be a seriously powerful tool for future problem solving.

1 Like

Thanks for the further suggestion peternlewis. Will try that out further tomorrow. Looks promising

Amazing! I finally wrapped my head around the whole 0x prefix business. It makes total sense now.

I would've been happy with just one solution. Now theres 4! Spoilt for choices here. Thanks a lot everyone. You guys are incredible.