|
|
@ -15,10 +15,11 @@ public static class BitUtls
|
|
|
|
/// <param name="scale">精度</param>
|
|
|
|
/// <param name="scale">精度</param>
|
|
|
|
/// <param name="round">保留几位小数</param>
|
|
|
|
/// <param name="round">保留几位小数</param>
|
|
|
|
/// <param name="offset">位偏移</param>
|
|
|
|
/// <param name="offset">位偏移</param>
|
|
|
|
|
|
|
|
/// <param name="unSign">对float和double 默认无符号</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <exception cref="ArgumentException"></exception>
|
|
|
|
/// <exception cref="ArgumentException"></exception>
|
|
|
|
public static object Bytes2Value(byte[] bytes, Type propertyType, int start, int length,
|
|
|
|
public static object Bytes2Value(byte[] bytes, Type propertyType, int start, int length,
|
|
|
|
double scale, int round, double offset)
|
|
|
|
double scale, int round, double offset, bool unSign = true)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (propertyType == BOOLEAN) return Convert.ChangeType(Byte2Bit(bytes, start, length) == 1, propertyType);
|
|
|
|
if (propertyType == BOOLEAN) return Convert.ChangeType(Byte2Bit(bytes, start, length) == 1, propertyType);
|
|
|
|
|
|
|
|
|
|
|
@ -44,11 +45,11 @@ public static class BitUtls
|
|
|
|
|
|
|
|
|
|
|
|
if (propertyType == FLOAT)
|
|
|
|
if (propertyType == FLOAT)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
Convert.ChangeType(Math.Round(Byte2Float(bytes, start, length, scale) - offset, round), propertyType);
|
|
|
|
Convert.ChangeType(Math.Round(Byte2Float(bytes, start, length, scale, unSign) - offset, round), propertyType);
|
|
|
|
|
|
|
|
|
|
|
|
if (propertyType == DOUBLE)
|
|
|
|
if (propertyType == DOUBLE)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
Convert.ChangeType(Math.Round(Byte2Double(bytes, start, length, scale) - offset, round),
|
|
|
|
Convert.ChangeType(Math.Round(Byte2Double(bytes, start, length, scale, unSign) - offset, round),
|
|
|
|
propertyType);
|
|
|
|
propertyType);
|
|
|
|
|
|
|
|
|
|
|
|
if (propertyType == STRING)
|
|
|
|
if (propertyType == STRING)
|
|
|
@ -220,46 +221,39 @@ public static class BitUtls
|
|
|
|
return list.ToArray();
|
|
|
|
return list.ToArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static float Byte2Float(byte[] bytes, int startBit, int length, double factor)
|
|
|
|
public static float Byte2Float(byte[] bytes, int startBit, int length, double factor, bool unSign)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (length < 17)
|
|
|
|
if (length < 17)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int d = Byte2UInt16(bytes, startBit, length);
|
|
|
|
int d =unSign ? Byte2UInt16(bytes, startBit, length) : Byte2Int16(bytes, startBit, length);
|
|
|
|
return (float)(d * factor);
|
|
|
|
return (float)(d * factor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var d = Byte2UInt32(bytes, startBit, length);
|
|
|
|
Int64 d = unSign ? Byte2UInt32(bytes, startBit, length) : Byte2Int32(bytes, startBit, length) ;
|
|
|
|
return (float)(d * factor);
|
|
|
|
return (float)(d * factor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static double Byte2Double(byte[] bytes, int startBit, int length, double factor)
|
|
|
|
public static double Byte2Double(byte[] bytes, int startBit, int length, double factor, bool unSign)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var d = Byte2UInt32(bytes, startBit, length);
|
|
|
|
Int64 d = unSign ? Byte2UInt32(bytes, startBit, length) : Byte2Int32(bytes, startBit, length);
|
|
|
|
return (float)(d * factor);
|
|
|
|
return (float)(d * factor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static ushort Byte2UInt16(byte[] bytes, int startBit, int length)
|
|
|
|
public static ushort Byte2UInt16(byte[] bytes, int startBit, int length)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
if (length < 9 || length > 16) throw new ArgumentException("length should be less then 17 and greater then 8");
|
|
|
|
|
|
|
|
|
|
|
|
var sub = Sub(bytes, startBit, length);
|
|
|
|
var sub = Sub(bytes, startBit, length);
|
|
|
|
if (sub.Length == 1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return Convert.ToUInt16(sub[0]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return BitConverter.ToUInt16(sub, 0);
|
|
|
|
return BitConverter.ToUInt16(sub, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static short Byte2Int16(byte[] bytes, int startBit, int length)
|
|
|
|
public static short Byte2Int16(byte[] bytes, int startBit, int length)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
if (length < 9 || length > 16) throw new ArgumentException("length should be less then 17 and greater then 8");
|
|
|
|
|
|
|
|
|
|
|
|
var sub = Sub(bytes, startBit, length);
|
|
|
|
var sub = Sub(bytes, startBit, length);
|
|
|
|
if (sub.Length == 1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return Convert.ToInt16(sub[0]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return BitConverter.ToInt16(sub, 0);
|
|
|
|
return BitConverter.ToInt16(sub, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|