Converting string to Hexadecimals codes

Hello,

A customer is working with this read/write IFM header:

https://www.ifm.com/es/en/product/DTI513

This IFM header send and receive Hexa codes to the PLC,

The customer has also a barcode reader that send an alphanumeric code to the PLC,

This alphanumeric is declared as a string in the code like for example "23TFU9",

The question is, can we convert this string to hexadecimal code so the IFM header can understand the code?

They would also like to read the hexa code that the IFM is sending and convert it to a string and I was wondering if there is a fb that can do this,

Thank you!

Best reply by HmiGuide

You can use the function IL_ByteToHexString from library CX_Utilities to convert a string into a HEX string of its ASCII codes.

(*
Converts a STRING into a HEX string of ASCII codes of characters
example: strHex := String2Hex('ABC'); // strHex := '414243'
*)

FUNCTION String2Hex : STRING(254);
VAR_INPUT
strText: STRING(127);
END_VAR

// Implementation
iLen := Len(strText);
ptrByte := ADR(strText);
String2Hex := '';
FOR i:=1 TO iLen DO
String2Hex := CONCAT(String2Hex, IL_ByteToHexString(ptrByte^));
ptrByte := ptrByte + 1;
END_FOR

View original
1 reply