【问题标题】:Converting a byte() array to a double in VB.Net在 VB.Net 中将 byte() 数组转换为 double
【发布时间】:2010-09-18 20:30:40
【问题描述】:

我有这种情况。 我有一个真实的存储在 sql 2005 数据库的 varbinary 字段中。 由于我无法在 sql 2005 中将 varbinary 转换为实数,因此我正在尝试在 vb.net 中执行此操作。

该字段作为 byte() 数组存储在 DataTable 中。

现在我想将该 byte() 读入双精度或十进制变量。 但我对如何做到这一点没有太多线索......

【问题讨论】:

    标签: vb.net arrays double decimal byte


    【解决方案1】:

    我不太了解 VB.net,但知道 .NET 库。

    将 byte[] 包装在 MemoryStream 中,并将其包装在 BinaryReader 中。然后使用 BinaryReader.ReadDouble() 方法。有关 MSDN 页面,请参阅 herehere

    编辑回复this

    您正在寻找如下所示的一段代码:

    'declare a test array
    Dim testArray As Byte() = {0, 0, 0, 0}
    'wrap it into a memory stream
    Dim memStream As MemoryStream = new MemoryStream(testArray)
    'wrap the stream in a binary reader
    Dim bReader As BinaryReader = new BinaryReader(memStream)
    'read a 32bit integer from the stream using the reader
    Dim count As Integer = bReader.ReadInt32()
    

    【讨论】:

      【解决方案2】:
      Public Function GetDateFromBytes(ByRef value() As Byte, _
                                          ByRef startindex As Int32) As Date
          'create a aray of Ints
          Dim IntValues() As Int32 = {BitConverter.ToInt32(value, startindex), _
                                          BitConverter.ToInt32(value, (startindex + 7)), _
                                          BitConverter.ToInt32(value, startindex + 15), _
                                           BitConverter.ToInt32(value, startindex + 31)}
      
          Return Date.FromBinary(New Decimal(IntValues))
      
      End Function
      

      【讨论】:

      • 对不起,我的上一个。代码完全错误。这里,下面,是正确的
      【解决方案3】:

      这真的取决于它的存储方式,但BitConverter.ToDouble 可能是你的朋友。假设它是 IEE754 格式。您首先从哪里获取数据?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多