Hi,
Quick question. I want to output the hex value of a number item. Is this possible.
Thanks
G
Hi,
Quick question. I want to output the hex value of a number item. Is this possible.
Thanks
G
In your rules you create an Integer there is a toHexString method.
new Integer(value).toHexString
On the sitemap you will need to create a new String Item to hold the hex string.
hmmâŠ
Sorry, but I canât figure out how to get this to work properlyâŠ
I want to convert Number item 1234, and use it as a HEX value as part of a string (serial binding).
Well shoot. For some reason the toHexString isnât there. Grumble.
This isnât easy at all. Hopefully something like this will work. Otherwise Iâm stumped:
val Number num = 1234
val java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(4)
bb.putInt(num.intValue)
val StringBuilder sb = new StringBuilder()
sb.append("0x") // optional if you want the format to be "0xAABBCCDD" verses "AABBCCDD"
bb.array.forEach(b | sb.append(java.lang.String.format("%02X", b)))
val hex = sb.toString
I will admit that Designer does not like this code at all. It is complaining about the second java.nio.ByteBuffer on the second line (but not the first) and acting like it doesnât know what it is but then it is just fine with the bb.putInt() so it is clearly seeing ByteBuffer. It is also telling me it doesnât know about String.format.
Iâm not in a place where I can test this out right now. Hope this works or gives you a path to follow.
I just end up with this error from the second line
Error during the execution of rule âTestruleâ: The name âjavaâ cannot be resolved to an item or type.
Humph.
You could try importing java.nio.ByteBuffer and removing the âjava.nioâ parts on the line it doesnât like.
If that doesnât work Iâm temporarily out of ideas. Back to Stackexchange.
Is the number guaranteed to be positive? We can obviously implement this directly but it would be better to not have to. But if we know it will be positive implementing it by hand will be easier.
I still get the
Error during the execution of rule âTestruleâ: The name âByteBufferâ cannot be resolved to an item or type.
Yes the value is always positive. How can I do that?
Thanks
Java methods like format
, are defined as static. As a result, their calls from XBase take a form more like:
String::format(...)
Otherwise youâll see errors. This is the same reason the toHexString()
example didnât work earlier.
This test Rule (validated) works correctly:
rule "Text Hex"
when
Time cron "0/10 * * * * ?"
then
var int upstairs_t = (NestTStatUpstairs_target_temperature_c.state as DecimalType).intValue()
logInfo("text-hex", String::format("As integer %d, as hex %s", upstairs_t, Integer::toHexString(upstairs_t)))
end
Youâll get output like:
12:52:40.003 DEBUG o.o.m.r.i.e.ExecuteRuleJob[:53]- Executing scheduled rule 'Text Hex'
12:52:40.012 INFO o.o.model.script.text-hex[:53]- As integer 20, as hex 14
Depending upon whether you need 0 prefixed Hex values are not, you can switch to using the String::format
style as needed.
Thanks, that worked!