Is there a VAIL procedure to convert hexadecimal to decimal?

I would like to know how to convert hexadecimal to decimal in a VAIL procedure.

I am working with a sensor device that only sends Hexadecimal to VANTIQ.

 

Posted: August 11, 2020 at 6:23 pm
Replies: 1
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
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
© Vantiq 2024 All rights reserved  •  Vantiq Corporation’s Privacy Policy and Cookie Policy