简体   繁体   English

在VB.Net中将byte()数组转换为double

[英]Converting a byte() array to a double in VB.Net

I have this situation. 我有这种情况。 I have a real stored in a varbinary field in a sql 2005 database. 我在sql 2005数据库的varbinary字段中存储了一个实数。 As I can't convert a varbinary to a real in sql 2005, I'm trying to do that in vb.net. 由于在sql 2005中无法将varbinary转换为实数,因此我尝试在vb.net中进行此操作。

That field gets stored as a byte() array in a DataTable. 该字段将作为byte()数组存储在DataTable中。

Now I would like to read that byte() into a double, or decimal variable. 现在,我想将该byte()读入双精度或十进制变量。 But I don't have much of a clue on how to do that... 但是我对如何做到这一点并不了解...

It really depends on how it's stored, but BitConverter.ToDouble may be your friend. 这实际上取决于它的存储方式,但是BitConverter.ToDouble可能是您的朋友。 That's assuming it's in IEE754 format. 假设它为IEE754格式。 Where are you getting the data from in the first place? 您首先从哪里获得数据?

I don't know VB.net well, but know the .NET libraries. 我不太了解VB.net,但了解.NET库。

Wrap the byte[] in a MemoryStream and wrap that in a BinaryReader. 将byte []包装在MemoryStream中,然后将其包装在BinaryReader中。 Then use the BinaryReader.ReadDouble() method. 然后使用BinaryReader.ReadDouble()方法。 See here and here for MSDN pages. 有关MSDN页面,请参见此处此处

Edit in response to this 编辑回应

You are looking for a piece of code looking like 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()
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM