Number as Hex

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. :expressionless:

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. :wink:

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.

2 Likes

Thanks, that worked!