異世界


2018年8月3日 星期五

[SNMP] Float32TC (OCTET STRING) (SIZE(4)) 資料格式轉換

private void TestFloat32TC ()
         {
             /* test : <Float32TC > */
             Float32TC OctSrt32 = new Float32TC();
             OctSrt32.Data = 39222.0F;

            List<byte> bL = OctSrt32.ByteList;
             byte[] bytes = OctSrt32.Bytes;

            listBox1.Items.Add(string.Format("OctSrt16.Data:{0}", OctSrt32.Data));

       }

#region  [SNMP] Float32TC (OCTET STRING) (SIZE(4))
/// <summary>
/// float(32bit) 39222.0
/// MemoryDump =>
///    Byte[]  => Byte[3]=0x47 ,Byte[2]=0x19 ,Byte[1]=0x36 ,Byte[0]=0x00
/// </summary>
public class FloatTC
{
     /* 欄位 */
     protected string m_OctetString = "";

    /* 方法 */
     //.. 將 Float or Double 做 Memory Dump
     public static string FloatToHexString(float source)
     {
         byte[] sourceArray = BitConverter.GetBytes(source);
         var number = BitConverter.ToUInt32(sourceArray, 0);
         string actual = number.ToString("X");
         return actual;
     }
     public static string FloatToHexString(double source)
     {
         byte[] sourceArray = BitConverter.GetBytes(source);
         var number = BitConverter.ToUInt64(sourceArray, 0);
         string actual = number.ToString("X");
         return actual;
     }

    //.. 將 MemoryDumpStr 轉 Float32 or Double64
     public static float HexStringToFloat(string source)
     {
         var number = int.Parse(source, System.Globalization.NumberStyles.AllowHexSpecifier);
         byte[] numberArray = BitConverter.GetBytes(number);
         float actual = BitConverter.ToSingle(numberArray, 0);
         return actual;
     }
     public static double HexStringToDouble(string source)
     {
         var number = int.Parse(source, System.Globalization.NumberStyles.AllowHexSpecifier);
         byte[] numberArray = BitConverter.GetBytes(number);
         double actual = BitConverter.ToSingle(numberArray, 0);
         return actual;
     }

    /* 屬性 */
     public List<byte> ByteList
     {
         get
         {
             var number = int.Parse(m_OctetString, System.Globalization.NumberStyles.AllowHexSpecifier);
             byte[] numberArray = BitConverter.GetBytes(number);
             return numberArray.ToList();
         }
     }
     public byte[] Bytes
     {
         get
         {
             var number = int.Parse(m_OctetString, System.Globalization.NumberStyles.AllowHexSpecifier);
             return BitConverter.GetBytes(number);
         }
     }
}
public class Float32TC : FloatTC
{
     /* 建構式 */
     public Float32TC() { m_OctetString = FloatToHexString(0.0F); }
     public Float32TC(float value) { m_OctetString = FloatToHexString(value); }
     public Float32TC(byte[] value)
     {
         var number = BitConverter.ToUInt32(value, 0);
         m_OctetString = number.ToString("X");
     }
     public Float32TC(List<byte> value)
     {
         var number = BitConverter.ToUInt32(value.ToArray(), 0);
         m_OctetString = number.ToString("X");
     }
     public Float32TC(string value)
     {
         float number = System.Convert.ToSingle(value);
         m_OctetString = number.ToString("X");
     }

    /* 屬性 */
     public float Data
     {
         get
         {
             if (m_OctetString.Length == 8) { return HexStringToFloat(m_OctetString); }
             else { throw (new Exception(string.Format("無法轉換 : [{0}]", m_OctetString))); }
         }
         set
         {
             m_OctetString = FloatToHexString(value);
         }
     }
}
public class Float64TC : FloatTC
{
     /* 建構式 */
     public Float64TC(){m_OctetString = FloatToHexString(0.0);}
     public Float64TC(double value) { m_OctetString = FloatToHexString(value); }
     public Float64TC(byte[] value)
     {
         var number = BitConverter.ToUInt64(value, 0);
         m_OctetString = number.ToString("X");
     }
     public Float64TC(List<byte> value)
     {
         var number = BitConverter.ToUInt64(value.ToArray(), 0);
         m_OctetString = number.ToString("X");
     }
     public Float64TC(string value)
     {
         double number = System.Convert.ToDouble(value);
         m_OctetString = number.ToString("X");
     }


     /* 屬性 */
     public float Data
     {
         get
         {
             if (m_OctetString.Length == 16) { return HexStringToFloat(m_OctetString); }
             else { throw (new Exception(string.Format("無法轉換 : [{0}]", m_OctetString))); }
         }
         set
         {
             m_OctetString = FloatToHexString(value);
         }
     }
}
#endregion

沒有留言:

張貼留言