Skip to content

Commit

Permalink
Fixed all conversion issues, fixes #96.
Browse files Browse the repository at this point in the history
  • Loading branch information
SeppPenner committed Jul 2, 2024
1 parent d3ff29b commit a98676c
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions src/SparkplugNet/Core/Extensions/DataTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,44 @@ internal static class DataTypeExtensions
}

// Check whether we need to convert from signed to unsigned.
var needToConvertFromSignedToUnsigned =
(objValue is sbyte sbyteVal && sbyteVal < 0) ||
(objValue is short shortVal && shortVal < 0) ||
(objValue is int intVal && intVal < 0) ||
(objValue is long longVal && longVal < 0);
if (objValue is sbyte sbyteVal && sbyteVal < 0)
{
return typeof(T) switch
{
Type t when t == typeof(uint) => (T)Convert.ChangeType(unchecked((uint)sbyteVal), typeof(T)),
Type t when t == typeof(ulong) => (T)Convert.ChangeType(unchecked((ulong)sbyteVal), typeof(T)),
_ => unchecked((T)objValue),
};
}

if (objValue is short shortVal && shortVal < 0)
{
return typeof(T) switch
{
Type t when t == typeof(uint) => (T)Convert.ChangeType(unchecked((uint)shortVal), typeof(T)),
Type t when t == typeof(ulong) => (T)Convert.ChangeType(unchecked((ulong)shortVal), typeof(T)),
_ => unchecked((T)objValue),
};
}

if (objValue is int intVal && intVal < 0)
{
return typeof(T) switch
{
Type t when t == typeof(uint) => (T)Convert.ChangeType(unchecked((uint)intVal), typeof(T)),
Type t when t == typeof(ulong) => (T)Convert.ChangeType(unchecked((ulong)intVal), typeof(T)),
_ => unchecked((T)objValue),
};
}

if (needToConvertFromSignedToUnsigned)
if (objValue is long longVal && longVal < 0)
{
return unchecked((T)objValue);
return typeof(T) switch
{
Type t when t == typeof(uint) => (T)Convert.ChangeType(unchecked((uint)longVal), typeof(T)),
Type t when t == typeof(ulong) => (T)Convert.ChangeType(unchecked((ulong)longVal), typeof(T)),
_ => unchecked((T)objValue),
};
}

// Check whether we need to convert from unsigned to signed.
Expand Down

0 comments on commit a98676c

Please sign in to comment.