Convert hexadecimal string to a number (Part 2)

Following on from the wiki entry in Part 1 - Convert hexadecimal string to a number (Part 1) - this Part 2 guide provides guidance for converting larger hexadecimal strings of up to six hex characters into their decimal equivalent. 

Full acknowledgement is given to @Aleš Berkafor the invaluable guide in Part 1.



Formula Block Download

The attached .Loxone file contains a matrix of Formula blocks to use for different applications. The explanation of which Formula block to use is described in this guide.

Hex to Decimal Formula Blocks.Loxone

Applicability

Loxone has a native capability to ingest hexadecimal strings using 'Command Recognition' \h. This will provide the correct decimal value in the Config for hex fields up to eight characters (the underlying decimal value must be between 2,147,483,647 and -2,147,483,647). The 'Command Recognition' \h is suitable when a value can be identified 'separately' in a returned Virtual Input request.



The use of Command Recognition \h is unsuitable when the desired hex characters are nested within a larger string of characters. Examples include equipment that sends data as a large character string representing numerous sensors. The following example is a hex string containing the values for 10 sensors received via a Virtual HTTP Input command -

Each sensor value is represented by four hexadecimal characters. In the following example, we wish to read Sensor 10, FF03.

1. Command Recognition needs to find the beginning of the hex character string by using the search term \i........\i. This 'jumps' the cursor to the unique and consistent text identified between the \i........\i

For the above example we will use \ivalue":"\i

This will place the 'cursor' directly after the quotation mark and at the beginning of the Sensor 1 value.

2. The beginning of the sensor field (FF03) then needs to be identified -

The commencement of FF03 is identified as the 36th character from where the 'cursor' has been placed previously -

\s36

3. The number of characters to injest then needs to be defined. This sensor uses four hex characters (FF03) and is identifed by using -

\4\3\2\1

The Complete Command Recognition

Combining the elements above produces the complete command recognition -

\ivalue":"\i\s36\4\3\2\1

The following picture shows the movement of a 'virtual cursor' in arriving at the desired field of characters -

Note that in some applications, the characters may need to have the order reversed or re-ordered. To reverse the values, swap \4\3\2\1 with \1\2\3\4. The config will then read the value in reverse order 30FF, instead of FF03.

To re-order the values, swap \4\3\2\1 with (for example) \2\1\4\3. This will result in 03FF.

The Result in Loxone

The Virtual Input retrieves the desired sensor field and presents it in the Config as follows -

However, the value returned in the Config is not the true decimal value that the sensor is sending. Instead, it is a representation of the hex values ingested by the Config. This value needs to be converted to the true decimal value using a Loxone Formula block.

Upper Case Hex and Lower Case Hex

We will use a Config Formula block to convert the ingested hex value to decimal. However, hex characters can be presented in either Upper Case or Lower Case. Mixed upper case and lower case would never normally be seen.

A different value is returned to the Config for Upper Case hex values versus the same characters in Lower Case. For this reason, two different Formula blocks are provided to suit applications that use the respective Upper Case or Lower Case.

Number of Hexadecimal Characters

In the preceeding example, the Command Recognition component \4\3\2\1 ingested 4 hex characters. A specific formula block is required to convert 4 characters. Different formula blocks are also provided to suit applications that require different numbers of hex characters.

A different application of Command Recognition might use \3\2\1. This will use the 3 character block.



Conclusion

When picking the appropriate Formula block from the above .Loxone file, a simple remedy is to pick the column with the applicable Upper case or Lower case hex, then work down the column to the applicable hex character number. Then copy the Formula block into the active project.



Background Information

The following .Loxone file is provided for background information only. It demonstrates the mechanism to derive the decimal number for a 6 character hex number.





The file includes a Constant with value 54156085834563. This value may need to be dragged into the Config where the 'Not Assigned' placeholder is. Connect the Constant to AI1 of each Hex digit block. This simulates a Virtual Input reading hex characters 1A2B3C.



The first formula decomposes the input value (54156085834563) into the ASCII decimal value for the applicable hex character -





The next formula blocks determine whether the input is in the range 0 to 9 or A to F -



The next formula block activates the A to F input (and nullifies the 0 to 9 input), or conversely, activates the 0 to 9 input (and nullifies the A-F input).

The result is then multiplied by the power applicable to the hex character position -



The decimal values for each character are then added for the total decimal value -





Finally, the entire page of formulas are nested together to create a single formula block.



...eventually leaves us with one Formula Block



Limitations with Command Recognition

The Config successfully ingests hex characters up to six characters. For seven characters (Command Recognition \7\6\5\4\3\2\1) and eight characters (Command Recognition \8\7\6\5\4\3\2\1), there appears to be a bug in the Config (as at version 11.1.9.14).

For 7 characters, the Virtual Input doesn't properly read the 1st hex digit (the least significant end).

For 8 characters, the Virtual Input doesn't properly read the 1st and 2nd hex digits (the least significant end).

The end.



Convert hexadecimal string to a number (Part 1)