?
Solved

convert single array to bytes array

Posted on 2009-05-15
5
Medium Priority
?
399 Views
Last Modified: 2012-05-07
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)
        Next
        lvPointer = lvPointer + 4
Next

Open in new window

0
Comment
Question by:sayeth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 46

Expert Comment

by:aikimark
ID: 24404633
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:
http://www.codeproject.com/KB/vb/netcopymemorysample.aspx


Look about half way down this page for an array copy example using System.Buffer, which keeps it all in managed space:
http://www.codeproject.com/KB/vb/CopyMemory_in_Net.aspx

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

0
 
LVL 1

Author Comment

by:sayeth
ID: 24425401
sorry for the late reply. i don't want to copy. i want to covert a single or a decimal array to byte array
0
 
LVL 46

Expert Comment

by:aikimark
ID: 24426056
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.

0
 
LVL 1

Author Comment

by:sayeth
ID: 24426182
yes, i understand what you saying.  i m trying to covert a single or a decimal array to byte array without any  loop
0
 
LVL 46

Accepted Solution

by:
aikimark earned 1000 total points
ID: 24426628
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question