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
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.
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
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
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
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 ?????
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)