Hex to string or integer on map

I have a map of hex values, it there a easy way to convert them to numbers, I'm currently doing
HexUtils.hexStringToInt(data[9])
But would like to do all the values in the map without having to code each one , [1], [2],[3],etc

I think you can do this with the Map function .collectEntries.

Let's say you have the map where both the key and value are strings, like this:

Map myMap = ["orange":"0xAB", "yellow":"0x10"]

To get a new map with the value now in integer, I think you do:

Map newMap = myMap.collectEntries{ key, value -> [ key : HexUtils.hexStringToInt(value)]

See: Collection (Groovy JDK)

Its not a map like that its just a list separated with comas

Based on how you accessed it in your example, I assume it's an array of hex strings.

You can do something like these. The first is my quick and dirty method. The second is like the one @jvm33 recommended (thanks for the tip!)

def traceRandomCode()
{
    def inputData = ["0xDE", "0xAD", "0xBE", "0xEF"]
    def outputData = []
    
    for(thisIn in inputData)
    {
        outputData.add(hubitat.helper.HexUtils.hexStringToInt(thisIn))
    }
    log.debug "${outputData}"
    
    def outputData2 = inputData.collect() {thisIn -> hubitat.helper.HexUtils.hexStringToInt(thisIn)}
    log.debug "${outputData2}"
}

If you have a list and just want to run a transformation on each item in the list you can use collect and do it in one line. Assuming your hex list is called colors you can transform to a new list (in the same order) using:

colors.collect{c -> ​HexUtils.hexStringToInt(c) }​
1 Like

And the new list would be.. c ?

sorry for leaving that off; you'd just assign the return value to whatever you want. collect returns the transformed data so you'd do something like:

def values = colors.collect{c -> ​HexUtils.hexStringToInt(c)}

1 Like

Thanks worked like a treat, shame I still can't make any sense of the data coming back

What's the bigger picture? What are you trying to do with the data, and what isn't working?

I'm trying to decode a tuya zigbee TRV
I've managed to find the setpoint reports but the rest ..