using System;
namespace S7.Net.Types
{
///
/// Contains the conversion methods to convert Real from S7 plc to C# double.
///
[Obsolete("Class Double is obsolete. Use Real instead for 32bit floating point, or LReal for 64bit floating point.")]
public static class Double
{
///
/// Converts a S7 Real (4 bytes) to double
///
public static double FromByteArray(byte[] bytes) => Real.FromByteArray(bytes);
///
/// Converts a S7 DInt to double
///
public static double FromDWord(Int32 value)
{
byte[] b = DInt.ToByteArray(value);
double d = FromByteArray(b);
return d;
}
///
/// Converts a S7 DWord to double
///
public static double FromDWord(UInt32 value)
{
byte[] b = DWord.ToByteArray(value);
double d = FromByteArray(b);
return d;
}
///
/// Converts a double to S7 Real (4 bytes)
///
public static byte[] ToByteArray(double value) => Real.ToByteArray((float)value);
///
/// Converts an array of double to an array of bytes
///
public static byte[] ToByteArray(double[] value)
{
ByteArray arr = new ByteArray();
foreach (double val in value)
arr.Add(ToByteArray(val));
return arr.Array;
}
///
/// Converts an array of S7 Real to an array of double
///
public static double[] ToArray(byte[] bytes)
{
double[] values = new double[bytes.Length / 4];
int counter = 0;
for (int cnt = 0; cnt < bytes.Length / 4; cnt++)
values[cnt] = FromByteArray(new byte[] { bytes[counter++], bytes[counter++], bytes[counter++], bytes[counter++] });
return values;
}
}
}