Aug 11, 2020
Posted by Ken

Unfortunately, there is no native/built-in way to convert a hexadecimal number to an integer or decimal. However, you can certainly do it with a VAIL procedure. I have included that procedure below.

PROCEDURE hexStringToInteger(hexString String)
//
// There is no built-in function for converting
// a hexadecimal number represented as a string to its binary equivalent.
// This procedure does just that.
//
if (startsWith(hexString, "0x") || startsWith(hexString, "0X")) {
   hexString = substr(hexString, 2) 
} 

var accum = 0
for (i in range(0, size(hexString), 1)) {
    accum = accum * 16
    var nextChar = substr(hexString, i, 1)
    if (nextChar == "a" || nextChar == "A") {
        accum += 10
    }
    else if (nextChar == "b" || nextChar == "B") {
        accum += 11     
    }
    else if (nextChar == "c" || nextChar == "C") {
        accum += 12
    }
    else if (nextChar == "d" || nextChar == "D") {
        accum += 13
    }
    else if (nextChar == "e" || nextChar == "E") {
        accum += 14
    }
    else if (nextChar == "f" || nextChar == "F") {
        accum += 15
    }
    else {
        accum += toInteger(nextChar)
    }
    log.debug("accumulated hex number is: {}", [accum])
}
return accum
© Vantiq 2024 All rights reserved  •  Vantiq Corporation’s Privacy Policy and Cookie Policy