Byte array problem . can't access elements of the array

I'm trying to work with a string that is a concatenation of positive bytes.
The string is: Zigbee Msg# 334A780F22EE0100
Where I would like to be able to test and get value for each byte:
33 4A 78 0F 22 EE 01 00

However I can't seem to address an individual element in the byte array.

Below is my code. You can see the last three "log.info" shows my latest attempts. I'm starting to think there may be something special of this type of array.

Does anyone see where I'm going wrong?
Thanks

def parse(String description){

    log.info  "Raw Description#$description"

//    Map map = [:]
    def event = zigbee.getEvent(description)

    //if (event) sendEvent(event)

    if (description?.startsWith('read attr -')){

        def descMap = zigbee.parseDescriptionAsMap(description)

        if (descMap.cluster == "0014" && descMap.attrId == "000E"){     // zigbee spec 0x000C= read analog cluster  0x0055 = present value
            ZigbeeHexMap = descMap.value
            log.info "Zigbee Msg# $ZigbeeHexMap"
            byte[] xarray = hubitat.helper.HexUtils.hexStringToByteArray(ZigbeeHexMap)
            log.info "fullarray=  $xarray"
            log.info "byteA=  $xarray(3)"
            log.info "byteB=  $xarray[3]"
            log.info "byteB=  $xarray:3"
        }   
    }
} // --- parse ---

xarray[3] = 0x78 = 120

Then I a would have thought the line:

log.info "byteB= $xarray[3]"

would have logged 0x78

instead of
byteB= [51, 74, 120, 15, 34, -18, 1, 0][3]

Ahhhh....
I see now for some reason the log.info does not like the array element but will print the correct value if I assign the element to a "byte myvalue" first.

Thank you.

Try:

log.info "byteB= ${xarray[3]}"

(Might still show it as 120 though).

2 Likes

This works too. Shows the hex value 15

Again thanks.

2 Likes

Duh, I missed the zero based array, you'd think after all these years...

:person_facepalming:

1 Like

I knew the array was zero based but until I could get individual elements I wasn't even thinking of which element I referenced.

1 Like