Hex Text to Number
hard
Reads a number a device sends as hex text, such as 'A1C8', and rejects text that is not hex.
What it teaches
A character is stored as its code: '0' is 48, '9' is 57, 'A' is 65 and 'a' is 97. Each digit is worth what it was in Byte to Hex Text, 0 to 15, with 'a' to 'f' worth the same as 'A' to 'F', so 'A' is 10, not 0. Each place is worth 16 times the place to its right, so '10' is 16 and '1F' is 1 × 16 + 15 = 31. One to four hex digits are valid; any other character, such as the G in '12G4', is not. STOD cannot help, because it reads decimal only.
Parameters
| Tag | Direction | Type | Default | Description |
|---|---|---|---|---|
| Out_Value | Output | DINT | 0 | The number Ref_Hex spells, or 0 |
| Sts_Valid | Output | BOOL | 0 | Ref_Hex is 1 to 4 hex digits |
| Ref_Hex | InOut | STRING | — | Hex digits as a device sends them |
Required behavior
- Devices often send numbers as hex text: 'A1C8' is 41416. Every scan, Out_Value is the number Ref_Hex spells, read as 1 to 4 hex digits, and Sts_Valid is 1.
- Lower case counts the same as upper case: 'a1c8' is 41416 too.
- Ref_Hex that is empty, longer than four characters, or holds any other character is not valid: Sts_Valid is 0 and Out_Value is 0.
- Never change Ref_Hex.
- With the default Ref_Hex of 'A1C8', Out_Value is 41416 and Sts_Valid is 1.