Skip to content

Commit 1705beb

Browse files
committed
Fix decoding of 64 bit values, as OR operation works only on 32 bit
1 parent fe30516 commit 1705beb

File tree

1 file changed

+9
-5
lines changed
  • src/S7CommPlusDriver/Core

1 file changed

+9
-5
lines changed

src/S7CommPlusDriver/Core/S7p.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,10 @@ public static int DecodeUInt64(System.IO.Stream buffer, out UInt64 value)
182182
value = 0;
183183
return 0;
184184
}
185-
value = (UInt64)((buffer.ReadByte() << 56) | (buffer.ReadByte() << 48) | (buffer.ReadByte() << 40) | (buffer.ReadByte() << 32) |
186-
(buffer.ReadByte() << 24) | (buffer.ReadByte() << 16) | (buffer.ReadByte() << 8) | buffer.ReadByte());
185+
byte[] b = new byte[8];
186+
buffer.Read(b, 0, 8);
187+
Array.Reverse(b, 0, 8);
188+
value = BitConverter.ToUInt64(b, 0);
187189
return 8;
188190
}
189191

@@ -194,8 +196,10 @@ public static int DecodeInt64(System.IO.Stream buffer, out Int64 value)
194196
value = 0;
195197
return 0;
196198
}
197-
value = (Int64)((buffer.ReadByte() << 56) | (buffer.ReadByte() << 48) | (buffer.ReadByte() << 40) | (buffer.ReadByte() << 32) |
198-
(buffer.ReadByte() << 24) | (buffer.ReadByte() << 16) | (buffer.ReadByte() << 8) | buffer.ReadByte());
199+
byte[] b = new byte[8];
200+
buffer.Read(b, 0, 8);
201+
Array.Reverse(b, 0, 8);
202+
value = BitConverter.ToInt64(b, 0);
199203
return 8;
200204
}
201205

@@ -466,7 +470,7 @@ public static int DecodeDouble(System.IO.Stream buffer, out double value)
466470
v[2] = (byte)buffer.ReadByte();
467471
v[1] = (byte)buffer.ReadByte();
468472
v[0] = (byte)buffer.ReadByte();
469-
value = BitConverter.ToDouble(v, 0); ;
473+
value = BitConverter.ToDouble(v, 0);
470474
return 8;
471475
}
472476

0 commit comments

Comments
 (0)