Cast a short array to byte array?

dbAny

New member
Joined
Apr 27, 2005
Messages
3
Programming Experience
10+
I would like to write the contents of a short array using the filestream writer for a byte array. I would like to do this to increase the speed I can write the array (am doing "real time" vb.net) Does anyone know how to cast the short array to byte array, or ANY method to get this to work?

Thanks,

Dale

Sub main()
Dim fo As New FileInfo("e:\dbtest.bin")
Dim fs As FileStream = fo_OpenWrite
Dim nBytes As Integer
Dim ByteArray(10) As Byte
Dim SArray(10) As Short
Dim ii As Short
Dim bb As Byte

bb = 0
For ii = 0 To 10
SArray(ii) = ii
ByteArray(ii) = bb
bb += 1
Next ii

nBytes = ByteArray.Length
fs.Write(ByteArray, 0, nBytes)
' Would like to comment out the above 2 lines
' and include 2 following lines
'nBytes = SArray.Length
'fs.Write(SArray, 0, nBytes)

End Sub
 

gripusa

Well-known member
Joined
Apr 29, 2005
Messages
73
Location
Lahore Pakistan
Programming Experience
1-3
Private Function ByteArrays()

Dim bt(10) As Byte

Dim st(10) As Short

Dim i As Integer

' assign values here


' then use the following method
bt.Copy(bt, st, bt.Length)

'do what ever u want to do
End Function

 

dbAny

New member
Joined
Apr 27, 2005
Messages
3
Programming Experience
10+
Still did not work

Thanks for the reply. I tried the following:

Dim bt(20) As Byte
Dim st(10) As Short
Dim s As Short
Dim b As Byte

For s = 0 To 10
st(s) = s
Next

For b = 0 To 10
bt(b) = b + 10
Next

' bt.Copy(bt, st, 10) ' works for Byte to Short
st.Copy(st, bt, 5) ' does not work Short to Byte

It works going from a byte array to a short, however it does not work from a short array to a byte array.

The reason I want to do something like this is to store the data as quickly as possible. I need to store ~ 10Mbytes every 5 seconds, and must complete the write (via flush/close) before I can continue in the code. I tried using 'fileput()', however this method takes 4X as long (0.8 seconds vrs 0.2 seconds) compares to 'write' (using a byte array). Any ideas?
 
Top Bottom