using System; namespace S7.Net.Types { /// /// Contains the conversion methods to convert DWord from S7 plc to C#. /// public static class DWord { /// /// Converts a S7 DWord (4 bytes) to uint (UInt32) /// public static UInt32 FromByteArray(byte[] bytes) { return (UInt32)(bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]); } /// /// Converts 4 bytes to DWord (UInt32) /// public static UInt32 FromBytes(byte b1, byte b2, byte b3, byte b4) { return (UInt32)((b4 << 24) | (b3 << 16) | (b2 << 8) | b1); } /// /// Converts a uint (UInt32) to S7 DWord (4 bytes) /// public static byte[] ToByteArray(UInt32 value) { byte[] bytes = new byte[4]; bytes[0] = (byte)((value >> 24) & 0xFF); bytes[1] = (byte)((value >> 16) & 0xFF); bytes[2] = (byte)((value >> 8) & 0xFF); bytes[3] = (byte)((value) & 0xFF); return bytes; } /// /// Converts an array of uint (UInt32) to an array of S7 DWord (4 bytes) /// public static byte[] ToByteArray(UInt32[] value) { ByteArray arr = new ByteArray(); foreach (UInt32 val in value) arr.Add(ToByteArray(val)); return arr.Array; } /// /// Converts an array of S7 DWord to an array of uint (UInt32) /// public static UInt32[] ToArray(byte[] bytes) { UInt32[] values = new UInt32[bytes.Length / 4]; int counter = 0; for (int cnt = 0; cnt < bytes.Length / 4; cnt++) values[cnt] = FromByteArray(new byte[] { bytes[counter++], bytes[counter++], bytes[counter++], bytes[counter++] }); return values; } } }