Fix all conversions

This commit is contained in:
Peter Butzhammer
2024-02-06 17:51:53 +01:00
parent baef1afac1
commit d422249955
2 changed files with 14 additions and 17 deletions

View File

@@ -16,41 +16,40 @@ namespace Sharp7.Rx
if (typeof(TValue) == typeof(int))
{
if (address.Length == 2)
return (TValue) (object) BinaryPrimitives.ReadInt16BigEndian(buffer);
return (TValue) (object) (int) BinaryPrimitives.ReadInt16BigEndian(buffer);
if (address.Length == 4)
{
return (TValue) (object) BinaryPrimitives.ReadInt32BigEndian(buffer);
}
throw new InvalidOperationException($"length must be 2 or 4 but is {address.Length}");
}
if (typeof(TValue) == typeof(long))
{
return (TValue) (object) BinaryPrimitives.ReadInt64BigEndian(buffer);
}
if (typeof(TValue) == typeof(ulong))
{
return (TValue) (object) BinaryPrimitives.ReadUInt64BigEndian(buffer);
}
if (typeof(TValue) == typeof(short))
{
return (TValue) (object) BinaryPrimitives.ReadInt16BigEndian(buffer);
}
if (typeof(TValue) == typeof(byte) || typeof(TValue) == typeof(char))
{
if (typeof(TValue) == typeof(byte))
return (TValue) (object) buffer[0];
}
if (typeof(TValue) == typeof(char))
return (TValue) (object) (char)buffer[0];
if (typeof(TValue) == typeof(byte[]))
{
return (TValue) (object) buffer;
if (typeof(TValue) == typeof(double))
{
var d = new UInt32SingleMap
{
UInt32 = BinaryPrimitives.ReadUInt32BigEndian(buffer)
};
return (TValue) (object) (double)d.Single;
}
if (typeof(TValue) == typeof(double) || typeof(TValue) == typeof(float))
if (typeof(TValue) == typeof(float))
{
var d = new UInt32SingleMap
{
@@ -71,9 +70,7 @@ namespace Sharp7.Rx
return (TValue) (object) Encoding.ASCII.GetString(buffer, 2, length);
}
else
{
return (TValue) (object) Encoding.ASCII.GetString(buffer).Trim();
}
throw new InvalidOperationException(string.Format("type '{0}' not supported.", typeof(TValue)));
}