Convert from decimal to hexadecimal in cmake

Y: Is it possible to take a variable A with 16 decimal places and convert it to 10 hexadecimal, and vice versa, to CMake?

A Google search only led me to the following:

http://public.kitware.com/pipermail/cmake/2008-September/024092.html

What does the conversion not do.

X: I am trying to use the "Configure File", reading the configured value as decimal and exposing it as hexadecimal in the configured header file.

+5
source share
2 answers

I rode on my own, but it looks like it should be inline

cmake_minimum_required (VERSION 2.6.4) macro(HEXCHAR2DEC VAR VAL) if (${VAL} MATCHES "[0-9]") SET(${VAR} ${VAL}) elseif(${VAL} MATCHES "[aA]") SET(${VAR} 10) elseif(${VAL} MATCHES "[bB]") SET(${VAR} 11) elseif(${VAL} MATCHES "[cC]") SET(${VAR} 12) elseif(${VAL} MATCHES "[dD]") SET(${VAR} 13) elseif(${VAL} MATCHES "[eE]") SET(${VAR} 14) elseif(${VAL} MATCHES "[fF]") SET(${VAR} 15) else() MESSAGE(FATAL_ERROR "Invalid format for hexidecimal character") endif() endmacro(HEXCHAR2DEC) macro(HEX2DEC VAR VAL) IF (${VAL} EQUAL 0) SET(${VAR} 0) ELSE() SET(CURINDEX 0) STRING(LENGTH "${VAL}" CURLENGTH) SET(${VAR} 0) while (CURINDEX LESS CURLENGTH) STRING(SUBSTRING "${VAL}" ${CURINDEX} 1 CHAR) HEXCHAR2DEC(CHAR ${CHAR}) MATH(EXPR POWAH "(1<<((${CURLENGTH}-${CURINDEX}-1)*4))") MATH(EXPR CHAR "(${CHAR}*${POWAH})") MATH(EXPR ${VAR} "${${VAR}}+${CHAR}") MATH(EXPR CURINDEX "${CURINDEX}+1") endwhile() ENDIF() endmacro(HEX2DEC) macro(DECCHAR2HEX VAR VAL) if (${VAL} LESS 10) SET(${VAR} ${VAL}) elseif(${VAL} EQUAL 10) SET(${VAR} "A") elseif(${VAL} EQUAL 11) SET(${VAR} "B") elseif(${VAL} EQUAL 12) SET(${VAR} "C") elseif(${VAL} EQUAL 13) SET(${VAR} "D") elseif(${VAL} EQUAL 14) SET(${VAR} "E") elseif(${VAL} EQUAL 15) SET(${VAR} "F") else() MESSAGE(FATAL_ERROR "Invalid format for hexidecimal character") endif() endmacro(DECCHAR2HEX) macro(DEC2HEX VAR VAL) if (${VAL} EQUAL 0) SET(${VAR} 0) ELSE() SET(VAL2 ${VAL}) SET(${VAR} "") WHILE (${VAL2} GREATER 0) MATH(EXPR VALCHAR "(${VAL2}&15)") DECCHAR2HEX(VALCHAR ${VALCHAR}) SET(${VAR} "${VALCHAR}${${VAR}}") MATH(EXPR VAL2 "${VAL2} >> 4") ENDWHILE() ENDIF() endmacro(DEC2HEX) 
+2
source

Using some tricks, you can do this quite simply. CMake expressions use int internally, so it only works up to 2 31 -1 bits. Something like this (you can change it for a big endian or something else).

 function (NumberToHex number output) set (chars "0123456789abcdef") set (hex "") foreach (i RANGE 7) math (EXPR nibble "${number} & 15") string (SUBSTRING "${chars}" "${nibble}" 1 nibble_hex) string (APPEND hex "${nibble_hex}") math (EXPR number "${number} >> 4") endforeach () string (REGEX REPLACE "(.)(.)" "\\2\\1" hex "${hex}") set ("${output}" "${hex}" PARENT_SCOPE) endfunction () 
+1
source

Source: https://habr.com/ru/post/1203948/


All Articles