• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 414
  • Last Modified:

convert single array to bytes array

i wrote a code that convert single array to bytes array - it work fine but i want a faster way because the bigger the single array gets the slower this code is
can any one help. thanks
Dim lvPointer, lvJ As Integer
Dim lvBlobBytes() As Byte
Dim lvBytes As Byte
Dim lvSingle() As single
ReDim lvBlobBytes(UBound(lvSingle) *4)
lvPointer = 0
For lvI = 0 To UBound(lvSingle)
      lvBytes = BitConverter.GetBytes(lvSingle(lvI))
       For lvJ = 0 To 3
             lvBlobBytes.SetValue(lvBytes(lvJ), lvPointer + lvJ)
        lvPointer = lvPointer + 4

Open in new window

  • 3
  • 2
1 Solution
You can do this with a single API call, although it will operate in unmanaged space.  Here is an example of the CopyMemory invocation from a VB.Net application:

Look about half way down this page for an array copy example using System.Buffer, which keeps it all in managed space:

Imports System.Buffer
Imports System.Text
Dim Str As String = "Adnan Samuel"
Dim MyBytesString() As Byte, i As Integer
Dim int(3) As short 'integer array
'Convert string to byte and copy to byte array
MyBytesString = Encoding.Default.GetBytes(Str)
'Use BlockCopy method to copy block of bytes array
'to integer array of specified length (8)
Buffer.BlockCopy(MyBytesString, 0, int, 0, 8)
'Print to output window

Open in new window

sayethAuthor Commented:
sorry for the late reply. i don't want to copy. i want to covert a single or a decimal array to byte array
I don't think you can bypass the copying of the data from one array to another.  What I've supplied are two methods to copy the data in one operation, rather than one (or more) operations per array item.

sayethAuthor Commented:
yes, i understand what you saying.  i m trying to covert a single or a decimal array to byte array without any  loop
I have linked you to two methods that will copy the data without looping.  I have done this with VB6 and can tell you it works as intended.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now