Hi B&R Community,
I’m currently working on a project involving UDP communication where I need to convert data between little-endian and big-endian formats. This is a common requirement when interfacing with external devices or protocols that use a different byte order.
I’m wondering if someone already develop or know a library to contains a generic function or function block that can perform this conversion for any variable by simply providing its memory address.
What I’m looking for:
- A pre-built function or function block that takes:
- A pointer to a variable (or its memory address).
- A destination buffer (array of bytes).
- A flag to specify the conversion direction (little → big or big → little).
- The function should automatically detect the data type (e.g.,
UDINT, REAL, STRUCT, etc.) and apply the correct byte-swapping logic.
- Ideally, it should handle nested structures and arrays without requiring manual intervention.
Why a generic solution would help:
- It would simplify the code and reduce the risk of errors when dealing with complex structures.
- It would save development time, especially when working with protocols that require endianness conversion.
- It would make the code more maintainable and reusable across different projects.
Example Use Case:
For instance, if I have a structure like this:
TYPE
ST_DataPacket : STRUCT
packetType: UDINT;
version: UDINT;
sequenceNumber: UDINT;
data: ARRAY[1..9] OF REAL;
END_STRUCT
END_TYPE
I’d like to call a function like this:
FB_EndianConverter(
pSource := ADR(myDataPacket),
pDest := ADR(byteBuffer),
toBigEndian := TRUE
);
And have the function automatically handle the conversion for all fields in the structure.
Questions:
- Does B&R Automation Studio provide a built-in function or function block for this purpose? (I’m pretty sure this didn’t exist)
- If not, has anyone in the community developed a generic solution for endianness conversion that they’d be willing to share?
- Are there any best practices or recommendations for handling endianness conversion in a way that’s both efficient and maintainable?
Made my own library:
- Using
PV_LKADDR, PV_XGETADR, PV_NINFO, and PV_ITEM to traverse structures and identify data types.
- Convert basic datatype (little → big or big → little)
Thanks in advance for your help!
Best regards,
Florent
Hi @florent.boissadier ,
from AI…hehe
”How can implement little-endian ↔ big-endian conversion in B&R automation studio?”
Answer
In B&R Automation Studio, endian conversion is typically handled manually using bitwise operations or pointer manipulation, as there isn’t a single universal “endian swap” function for all data types.
Implementation Methods
-
Bitwise Shifting (Structured Text)
For simple integers, you can manually isolate and move bytes using AND, OR, and bit-shifts (SHL, SHR).
-
16-bit (UINT): (val AND 16#00FF) SHL 8 OR (val AND 16#FF00) SHR 8.
-
32-bit (UDINT): Mask each byte (e.g., 16#FF000000) and shift it to its mirrored position (24 bits right), then OR the results together.
-
Pointer and Byte Array Manipulation (C or Structured Text)
Treat the variable as an array of bytes and reverse their order.
-
Structured Text: Use a pointer to an array of bytes (ARRAY[0..3] OF USINT) that points to the same memory address as your variable. Swap indices 0 with 3 and 1 with 2 for a 4-byte variable.
-
ANSI C: Use a simple loop to reverse the bytes in a memory buffer.
-
Communication-Specific Conversions
-
Modbus: B&R libraries often handle standard Modbus byte order automatically, but for 32-bit values (REAL/DINT), you may need to swap the two 16-bit registers (word swap).
-
Profinet: If communicating with a Big-Endian master, you must explicitly swap the bytes of complex types like floats before sending.
Performance Tip
If you are developing in ANSI C, many compilers in Automation Studio support __builtin_bswap32() or similar intrinsic functions that map directly to the CPU’s native byte-swap instruction for maximum efficiency.
———————–
I’m positive we have already a user library for that
Ciao
Valerio
Thanks for the quick answer ^^
To be honest I know it’s possible and how it could be implemented in Automation Studio, I’m just trying to save developing time 
You are saying that there is a user library for that, by any change there is a way to access it?
Regards,
Florent
Thanks! That’s part of what I’m searching, I would like to be able to give as parameter user defined structure
Hi @florent.boissadier I have found the library but now I see we have standard conversion functions in AS (not available 10 years ago) so please use them.
Ciao
Valerio
So I need to make my own lib to traverse structures then use standard conversion on each basic datatype I’m seeing during the traverse, I’m right?
Yes you are correct.
You need to know the size of the element you are swapping.. So if you want to make it reusable then you need to read out the structure and get the types and call the convert for that. So basically with the information from PV_ninfo you could build it as mentioned in your start post.
2 Likes
If anyone is interested, I created my own ANSIC function to handle ToBigEndian and FromBigEndian using the H_TO_N and N_TO_H functions from AsIecCon.
By taking the address as the input variable, only single-dimensional arrays are supported, and the array must start at index 0.