banner
Home News

A weekly programming example: the magical conversion of hexadecimal to floating point numbers

A weekly programming example: the magical conversion of hexadecimal to floating point numbers

Aug 18, 2025

•Introduction: In industrial automation, PLC engineers often need to process raw data transmitted from low-level devices. When faced with the need to combine two 16-bit registers and interpret them as a 32-bit floating-point number (REAL), how can this be achieved efficiently? This article will use a sophisticated double-word conversion technique to uncover the secrets of hexadecimal-to-floating-point conversion in PLCs.

 

•Program Code and Analysis Below is the complete PLC program code (based on the ST language of the CodeSys platform): 1 FUNCTION_BLOCK FB_HexToReal

2 VAR_INPUT 3 HWd: WORD; // High word (e.g., register 40001) 4 LWd: WORD; // Low word (e.g., register 40002)

5 END_VAR

6 VAR_OUTPUT 7 RealOutput: REAL; // Output floating-point number

8 END_VAR

9 VAR 10 hexLWd: DWORD; // Temporary high double word 11 hexDw: DWORD; // Combined double word 12 pConverter: POINTER TO REAL; // Type conversion pointer

13 END_VAR

14 // --- Core conversion logic ---

15 hexLWd := HWd;               // Store the high-order word into a double-word variable

16 hexDw := SHL(hexLWd, 16) + LWd;  // Shift the high-order word left by 16 bits and concatenate it with the low-order word

17 pConverter := ADR(hexDw);     // Get the double-word memory address

18 RealOutput := pConverter^;    // Reinterpret the value as a floating-point number using a pointer

 

• Key Technical Points Analysis:

Double-word concatenation:

SHL(hexLWd, 16) + LWd shifts the high-order word left by 16 bits and then adds it to the low-order word to form a complete 32-bit double-word (DWORD). Memory Reinterpretation:

Directly read double-word memory using the POINTER TO REAL pointer and interpret its binary contents as IEEE 754 floating-point numbers.

No Conversion Loss:

Compared to traditional mathematical conversion, this method operates directly on memory, resulting in zero precision loss and extremely high execution efficiency.

 

• Typical Application Scenarios

Device Communication Parsing

Parse floating-point numbers (occupying two consecutive registers) transmitted from Modbus/TCP devices, such as temperature transmitters and flow meter data.

Host Computer Data Interaction

Process floating-point data packets sent by SCADA systems, such as setpoints and PID parameters.

Histor Data Reconstruction

Read floating-point historical data stored as two WORDs from a database and restore it to actual engineering values. Example: A temperature control system reads HWd = 16#4120 and LWd = 16#0000 from a sensor. After conversion, this program outputs RealOutput = 10.0 (degrees Celsius).

 

• Security Enhancement: Add range checking to prevent illegal values (such as NaN):

1 IF hexDw <> 16#FFC00000 THEN // Exclude NaN

2 RealOutput := pConverter^;

3 ELSE 4 RealOutput := 0.0;

5 END_IF

• Support for Multiple Data Types: Expand the function block to support LREAL (64-bit floating-point numbers):

1 hexDw := SHL(HWd, 48) + SHL(MWd, 32) + SHL(LWd_H, 16) + LWd_L;

• Byte Order Issues: If the device byte order is inconsistent with the PLC, adjust the concatenation order:

1 hexDw := SHL(LWd, 16) + HWd; // Little-Endian Mode

 

• Summary

The FB_HexToReal function block demonstrated in this article uses memory pointer reinterpretation technology to achieve lossless and efficient conversion of two 16-bit registers to floating-point numbers. This method: Fast execution: Only four key instructions are required; Low resource usage: No additional mathematical operations; Guaranteed precision: Avoids rounding errors associated with traditional multiplication and division. This "binary literal translation" approach can significantly improve development efficiency in scenarios such as industrial communication protocol parsing and cross-system data exchange. Mastering memory manipulation techniques will greatly enhance your PLC programming skills!

Leave A Message

Leave A Message
If you are interested in our products and want to know more details,please leave a message here,we will reply you as soon as we can.
Submit
Contact Us:justwellautomation@qq.com

Home

Products

whatsApp

contact