CMake hex2dec and dec2hex

Here we exploit the convention that 0x prefix on a number implies it’s a hexadecimal number.

CMake hex2dec

cmake_policy(VERSION 3.13)

math(EXPR decval "0x${hexval}" OUTPUT_FORMAT DECIMAL)

message(STATUS "Hex: ${hexval} => decimal: ${decval}")

running

cmake -Dhexval=14 -P hex2dec.cmake

yields:

– Hex: 14 => decimal: 20