Solved

Shifting Byte arrays?

Posted on 1999-01-21
19
280 Views
Last Modified: 2010-05-03
Experts...I need to shift a byte array say 10, 20 or whatever number of elements up to make room for other information such as a header.  How can I do this WITHOUT using a loop of some kind.  CopyMemory?  Sample code that works please...

PS  It's gotta be really FAST
0
Comment
Question by:Johnn
  • 8
  • 6
  • 3
  • +2
19 Comments
 
LVL 10

Expert Comment

by:caraf_g
ID: 1470003
This could be very easy if you happen to be using VB6

In VB6 you are allowed to assign the value of a byte array to a string and vice versa.

Simply move your byte array to a string, concatenate the 10 characters and move it back.
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1470004
I think the questioner meant something else. He want to move memory.

Here is a sample:
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)


 Call CopyMem(udtVerBuffer, ByVal lplpBuffer, Len(udtVerBuffer))

0
 
LVL 1

Author Comment

by:Johnn
ID: 1470005
What I want to do is "insert" bytes to the beginning of an existing byte array, not append...
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1470006
Hello Mirkwood,

As I read the question, Johnn's byte array might contain something line "ABC" (looking at byte array as if it were a string)

Now what Johnn wants to do is insert 10 "blanks" before:
"          ABC" so that he has room to put in some "header" information.

In VB6 this can be achieved like I said.

Johnn, if you haven't got VB6 I think Mirkwood's suggestion might be the best alternative.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1470007
"concatenate the 10 characters"

or of course:

concatenate it to 10 characters (to insert it before)

0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1470008
caraf_g:
It could be that the byte array is a string but you never know.
Btw: VB5 support also the assigning byte array to string and vice versa as far as I rememeber. The problem with this that this is different under NT and win95 because of unicode.


0
 
LVL 1

Author Comment

by:Johnn
ID: 1470009
The solution you posted(VB5 works as well) works for all strings, however I'm mixing string and numeric data.  No that I've played with the idea I might want something like this...
dim myByte() as byte
redim myByte(0 to 3)'four byte long header info, aka length of string
mybyte(0)=0
mybyte(1)=0
mybyte(2)=5
mybyte(3)=1
'now I want to "append" to this byte array a string, how do we do it without a loop? OR we could build the string byte array first and then insert this in front...
0
 
LVL 1

Author Comment

by:Johnn
ID: 1470010
How does that unicode affect things?  Which os uses which?  From my tests(win95), assigning a string to a byte array doubles it which is not something I want(I know it recreates it properly, but we could be talking alot of loop cycles here)
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1470011
It depends on your string and OS.
In general win95 uses 1 byte characters
and winnt uses 2 byte characters
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:Johnn
ID: 1470012
are you sure you didn't get the two os' mixed up?  I'm using win95 and my bytearrays were double the size of the string.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1470013
Hello Johnn,

Can you reject my answer please - it looks like Mirkwood is getting close to having a solution for you.
0
 
LVL 1

Author Comment

by:Johnn
ID: 1470014
as requested, although neither is close yet...
0
 
LVL 3

Expert Comment

by:vikiing
ID: 1470015
>>How does that unicode affect things?

Unicode uses a word (16 bits = TWO bytes) for each character.


0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1470016
VB5 Internally uses unicode or at least 2-byte strings: See following code.
This code prefixes 4 bytes to an existing byte array. But to make sure I set sensible data I set the first two bytes to 65 and 0 (making up a capital A in unicode) and the second two bytes to 66 and 0 (making up a capital B)


Dim barTEST() As Byte
Dim barTEST2(1 To 4) As Byte

Dim strTEST1 As String
Dim strTEST2 As String

strTEST2 = "Hello"
barTEST = strTEST2

MsgBox barTEST

barTEST2(1) = 65
barTEST2(2) = 0
barTEST2(3) = 66
barTEST2(4) = 0
strTEST1 = barTEST2

strTEST2 = strTEST1 & strTEST2
barTEST = strTEST2

MsgBox barTEST


0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1470017
PS - I'm running Windows NT 4.0 + SP3
0
 
LVL 10

Accepted Solution

by:
caraf_g earned 10 total points
ID: 1470018
Hmmm..

I think my suggestion does work after all. Check this out:

Option Explicit

Private Sub Command1_Click()

'Give barOriginal an odd number of bytes to prove that _
 even though internal VB strings are supposedly unicode _
 (i.e. 2 bytes per character) this will not impact on how _
 the strings can be used to manipulate whole byte arrays _
 in one go.
 
Dim barOriginal(1 To 3) As Byte
Dim barNew() As Byte
Dim strWork As String
Dim strWork2 As String

'Give the individual elements some odd values which would _
 not make up decent string characters to prove that even _
 that will not cause any problems.
barOriginal(1) = 1
barOriginal(2) = 0
barOriginal(3) = 3
strWork = barOriginal
strWork2 = barOriginal
strWork = strWork & strWork2
barNew = strWork

MsgBox (barNew(0))
MsgBox (barNew(1))
MsgBox (barNew(2))
MsgBox (barNew(3))
MsgBox (barNew(4))
MsgBox (barNew(5))

'Original byte array had only 3 elements so concatenating _
 it to itself makes a total of 6 elements so this next _
 one (the 7th) creates a subscript out of range error.
MsgBox (barNew(6))

End Sub

0
 
LVL 15

Expert Comment

by:ameba
ID: 1470019
If you want it fast, and you don't need any Wide character, use this mapping:
1 character (2 bytes) <-> 1 byte
Using CopyMemory is >2x faster than copying array members.

Paste this to your form:

Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, source As Any, ByVal bytes As Long)
Private Declare Sub ZeroMemory Lib "kernel32" Alias "RtlZeroMemory" (dest As Any, ByVal bytes As Long)
Dim myByte() As Byte

Private Sub Form_Click()
    ReDim myByte(0 To 5) 'four byte long header info, aka length of string
    myByte(0) = 1
    myByte(1) = 1
    myByte(2) = 5
    myByte(3) = 5
    myByte(4) = 7
    myByte(5) = 7
    'we could build the string byte array first and then insert this in front...
    Dim mystring As String, mylen As Long, ub As Long
    ub = UBound(myByte)
    mystring = "A A"
    mylen = Len(mystring)
    ReDim Preserve myByte(0 To ub + mylen)
    report
    CopyMemory ByVal VarPtr(myByte(mylen)), ByVal VarPtr(myByte(0)), ub + 1
    ZeroMemory ByVal VarPtr(myByte(0)), mylen
    report
' insert string
    CopyMemory myByte(0), ByVal mystring, mylen
    report
End Sub

Private Sub report()
    Dim i As Long, msg As String
    For i = 0 To UBound(myByte)
        msg = msg & myByte(i) & ","
    Next
    Debug.Print msg
End Sub

0
 
LVL 1

Author Comment

by:Johnn
ID: 1470020
I see your invading my other thread! :) I'm looking into your solution...thanks
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1470021
Johnn,

I'm a bit confused now. You've just accepted my answer but from your comments it looks like you're responding to ameba's comments.

I don't mind getting a few points but maybe in this case they should have gone to ameba? Or did you genuinely intend to accept my answer?
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now