I'm getting a byte[] response from a service, and I'm trying to convert this byte[] to a struct in c#, but one of the values which I have is 4294967.295 or 2147483.647 and when im defining long, double etc in my response struct, I'm getting it as NaN, or the value is incorrect, any inputs on what the datatype should be for such values?
My Struct on c#, and the struct given by the api and the extension used to cast the byte array to struct
using System.Globalization;
using System.Runtime.InteropServices;
namespace SampleExample
{
[StructLayout(LayoutKind.Sequential)]
public struct ParameterResponse
{
public uint Descriptor;
[MarshalAs(UnmanagedType.R4, SizeConst = 4)]
public float Value;
//public float Minimum;
//public float Maximum;
[MarshalAs(UnmanagedType.R4, SizeConst = 4)]
public float Minimum;
[MarshalAs(UnmanagedType.R4, SizeConst = 4)]
public float Maximum;
public uint Default;
public ushort Next;
public ushort Previous;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] Units;
public ushort Multiplier;
public ushort Divisor;
public ushort Base;
public ushort Offset;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] Link;
public byte pad;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] Name;
public ParameterResponse(uint descriptior, uint value, float maximum, float minimum, uint defaultvalue, ushort next, ushort previous,
byte[] units, ushort multiplier, ushort divisor, ushort baseValue, ushort offset, byte[] link, byte padValue, byte[] name)
{
Descriptor = descriptior;
Value = value;
Maximum = maximum;
Minimum = minimum;
Default = defaultvalue;
Next = next; Previous = previous;
Units = units;
Multiplier = multiplier;
Divisor = divisor;
Base = baseValue; Offset = offset;
Link = link;
pad = padValue;
Name = name;
}
}
}
uint32_t Descriptor;
uint32_t Value;
uint32_t Minimum;
uint32_t Maximum;
uint32_t Default;
uint16_t Next;
uint16_t Previous;
char Units[4];
uint16_t Multiplier;
uint16_t Divisor;
uint16_t Base;
uint16_t Offset;
char Link[3];
char pad;
char Name[16];
public static T CastToStruct<T>(this byte[] data) where T : struct
{
var pData = GCHandle.Alloc(data, GCHandleType.Pinned);
var result = (T)Marshal.PtrToStructure(pData.AddrOfPinnedObject(), typeof(T));
pData.Free();
return result;
}
var resultInStruct = byteOutput.CastToStruct<ParameterResponse>();