calimero-project / calimero-core

Core library for KNX network access and management
Other
126 stars 64 forks source link

2 bytes floating point #87

Closed ViorelOnica closed 5 years ago

ViorelOnica commented 5 years ago

Hello, on my group monitor, I'm saving the received values to a hex string (called triggered_data below).

String triggered_data=DataUnitBuilder.toHex(e.getASDU(), "");

For example, If I write a value of 17.5 on a 2 bytes floating point, triggered_data will be "06d6". Is there any way I can convert the hex string ( "06d6" ) to float ( 17.5 ) ? I know there is a solution using DPTXlator2ByteFloat floatTranslator, but I need to convert to float by a hex string parameter, not a byte[] parameter.

calimero-project commented 5 years ago

There is currently no inverse fromHex in DataUnitBuilder. Just copy one of the exisiting methods which does that to your source code (e.g., this one is from the introduction repo):

static byte[] fromHex(final String hex) {
    final int len = hex.length();
    final byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2)
        data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));
    return data;
}

The resulting byte array can be used with a translator.

ViorelOnica commented 5 years ago

Thank you !