Skip to content

Commit

Permalink
fix: Fix casting to byte for integer greater than 127 (byte is signed…
Browse files Browse the repository at this point in the history
… number and for string greater than 127 characters it result to NegativeIndexException) (#339)
  • Loading branch information
alessandromnc94 committed Apr 7, 2022
1 parent 5c16ee0 commit 6f5399e
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2032,8 +2032,8 @@ public static void serializeS7Char(WriteBuffer io, PlcValue value, String encodi
* the String as char arrays from your application.
*/
public static void serializeS7String(WriteBuffer io, PlcValue value, int stringLength, String encoding) {
byte k = (byte) ((stringLength > 250) ? 250 : stringLength);
byte m = (byte) value.getString().length();
int k = 0xFF & ((stringLength > 250) ? 250 : stringLength);
int m = 0xFF & value.getString().length();
m = (m > k) ? k : m;
byte[] chars = new byte[m];
for (int i = 0; i < m; ++i) {
Expand All @@ -2042,8 +2042,8 @@ public static void serializeS7String(WriteBuffer io, PlcValue value, int stringL
}

try {
io.writeByte(k);
io.writeByte(m);
io.writeByte((byte)(k & 0xFF));
io.writeByte((byte)(m & 0xFF));
io.writeByteArray(chars);
} catch (SerializationException ex) {
Logger.getLogger(StaticHelper.class.getName()).log(Level.SEVERE, null, ex);
Expand Down

0 comments on commit 6f5399e

Please sign in to comment.