Hex string to raw hex - HELP ME! (please) - Problem resolved

Hi Everyone,

I am writing a device driver and have reached the limit of my groovy knowledge. I am trying to convert a hex string to raw hex.

I need to send a code to the device in hex, eg: 8fcd4664270001040000.
so, if I add the line:

def newCode = 0x8fcd4664270001040000 (no quotes)

and then send newCode to the device it accepts it. I have written a handler (convertCode) to calculate the required code which comes up with the answer as a string "0x8fcd4664270001040000". I then change the above code to

def newCode = convertCode (state.lastUnrecognisedCode)
log.debug newCode

The logger reports "0x8fcd4664270001040000" as above but sending to the device doesn't work. I have discovered that the following code:

	def newCode = "0x8fcd4664270001040000"
	log.debug "Hex as string = $newCode"
	newCode = 0x8fcd4664270001040000
	log.debug "Raw hex = $newCode"

gives an output of
debug Raw hex = 679085061807431020183552
debug Hex as string = 0x8fcd4664270001040000

showing me that I am missing a line of code to convert my string into raw hex and this is where I reach the limit of my groovy ability. Could someone please help me with the line of code needed? I though that newCode = newCode as hex might be the solution but it is not that easy unfortunately.

Full topic on the device driver can be found here:
Zipato keypad - Device driver topic

1 Like

Does the hubitat.helper.HexUtils class help? I don't know what variable type your 0x value without quotes is in, but convert to byte array might work?
https://docs.hubitat.com/index.php?title=HexUtils_Object

Hi @cometfish,
I am now being really thick! I have tried

	newCode = convertCode (state.lastUnrecognisedCode)
	log.debug "Hex as string = $newCode"
	newCode = hexStringToByteArray(newCode)
	log.debug "Afterconversion = $newCode"

and it crashes with:
errorgroovy.lang.MissingMethodException: No signature of method: user_driver_mhutchy_Z_Wave_Zipato_Keypad__ported__387.hexStringToByteArray() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [8f9b4828855001040000] on line 535 (associate9)

for info, I don't think the 0x is important, the hardcoded works with or without but I read somewhere that it should be included to tell the HUB that what comes next is in Hex format

You need to put the full class name in:
newCode = hubitat.helper.HexUtils. hexStringToByteArray(newCode)

edit: and I'd leave out the 0x in the string

1 Like

ok, thanks,

Just tried that, thanks :blush:

In fact, I already have my hexString "8fcd4664270001040000", what I want is just hex (not as a string) and I can't figure our how to do it.

logDebug "8fcd4664270001040000" gives 8fcd4664270001040000
but logDebug 8fcd4664270001040000 gives 679085061807431020183552

def newCode 8fcd4664270001040000 gives an error but
def newCode 0x8fcd4664270001040000 works

I am trying to convert from the 1st to the 2nd. it is like "123" is a string that looks like an int and 123 is an int. to use "123" as in int we need to tell the hub

def myIntString = "123"
def myInt = myIntString as int

I think that the hub is seeing newCode as a pure string and not as a hexString and I don't know how to say myHex = newCode as hexString for example

... and what happened? :slightly_smiling_face:
Did you try sending a byte array to the device? If that didn't work, try sending the result of hexStringToInt to the device.

(I think your 0x format might be stored as an int anyway, despite how it's outputting in the debug log, at least the groovy docs seem to hint that)

Hi yes,

Tried that and the code works (but not the result I am looking for)

When sending the byte array to the device it sees it as a list of ASCII characters and stores the ascii code for each character in HEX format. they guy who wrote the driver for smartthings had the same problem with this device and sent it as raw hex to get it to work.

The problem I have is the code to send the raw hex works on ST if it is coded as a string but only works on hubitat if it is hard coded as I have described, ie: def newCode = 0x8fcd4664270001040000

	     def cmd = [
    	0x63, // USER_CODE command class
        0x01, // USER_CODE SET command
        userNumber,
        UserCodeSet.USER_ID_STATUS_OCCUPIED]
        cmd += newCode
        cmd = cmd.collect { String.format("%02x", it).toUpperCase() }.join('')
        requests << cmd

the String.format("%02x", it) crashes if it is a hexstring but not if it is pure hex. Smartthings accepts the hexstring but hubitat doesn't hense why I need to get a non-string version. If it stores it as an integer then I need to find out what the conversion is from hexString to hex stored in integer format so that I can replicate it in my code.

ie how do I get from 8fcd4664270001040000 to 679085061807431020183552 ????? :worried:

Ok I had a quick play around - the integer is too small to hold that number so it wasn't working.
This code gives you that number from that hex string:
new BigInteger("8fcd4664270001040000", 16)

3 Likes

BRILLIANT!!

Thanks so much for your help, driver is now working. @cometfish You're a star!

:clap:

3 Likes