For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > November 2005 > Copymemory Byte array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Copymemory Byte array
Simon Woods

2005-11-29, 6:55 pm

Hi

I've a byte array dim'ed 0 to 33 and it's populated with stuff up to element
31. I want to copy elements 0-31 into a string variable.

What's the copy memory call to do this?

Thanks

S



alpine

2005-11-29, 6:55 pm

On Tue, 29 Nov 2005 15:43:53 -0000, "Simon Woods"
<simonSPAMMENOT.woods@virginNOTMESPAM.net> wrote:

>Hi
>
>I've a byte array dim'ed 0 to 33 and it's populated with stuff up to element
>31. I want to copy elements 0-31 into a string variable.
>
>What's the copy memory call to do this?
>
>Thanks
>
>S



Assuming you're using the standard CopyMemory declare and your
destination is another byte array....

Call CopyMemory(abytYourDest(0), abytYourArray(0), 32&)


HTH,
Bryan
_______________________________
Bryan Stafford
New Vision Software
newvision_don'tspam@mvps.org
Simon Woods

2005-11-29, 6:55 pm

I'm looking to get it into a string variable ... can I not do something like
this ... I know I can't because this doesn't work !!

l_sRet = Space$(j)
CopyMemory StrPtr(l_sRet), l_abyOutput(0), j

Thanks

S

"alpine" <alpine_don'tsendspam@mvps.org> wrote in message
news:tquoo1tu3kjbjh0umc7bpb323ae22sg3bb@
4ax.com...
> On Tue, 29 Nov 2005 15:43:53 -0000, "Simon Woods"
> <simonSPAMMENOT.woods@virginNOTMESPAM.net> wrote:
>
>
>
> Assuming you're using the standard CopyMemory declare and your
> destination is another byte array....
>
> Call CopyMemory(abytYourDest(0), abytYourArray(0), 32&)
>
>
> HTH,
> Bryan
> _______________________________
> Bryan Stafford
> New Vision Software
> newvision_don'tspam@mvps.org



Ken Halter

2005-11-29, 6:55 pm

"Simon Woods" <simonSPAMMENOT.woods@virginNOTMESPAM.net> wrote in message
news:%23MrhBAQ9FHA.3360@TK2MSFTNGP11.phx.gbl...
> I'm looking to get it into a string variable ... can I not do something
> like this ... I know I can't because this doesn't work !!
>
> l_sRet = Space$(j)
> CopyMemory StrPtr(l_sRet), l_abyOutput(0), j
>
> Thanks
>
> S


You've already ruled out the use of StrConv?
'==========
Private Sub Command1_Click()
Dim b(33) As Byte
Dim s As String
Dim i As Integer

For i = 0 To 33
b(i) = i + 65
Debug.Print Chr$(b(i));
Next
Debug.Print

s = StrConv(b, vbUnicode)
Debug.Print Left$(s, 32) 'the byte array minus 1 element

End Sub
'==========

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..


Simon Woods

2005-11-29, 6:55 pm

If I'm honest - I wanted to avoid the Left$ for speed (I know it's
negligable), and so copy it directly into the string's allocated memory. I
recognise that the Space$ call may well create temporary strings any way and
so I'd lose any speed gains over Left$!!!

Just trying to understand ... that's all

Thanks



"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:el8FWEQ9FHA.4084@TK2MSFTNGP10.phx.gbl...
> "Simon Woods" <simonSPAMMENOT.woods@virginNOTMESPAM.net> wrote in message
> news:%23MrhBAQ9FHA.3360@TK2MSFTNGP11.phx.gbl...
>
> You've already ruled out the use of StrConv?
> '==========
> Private Sub Command1_Click()
> Dim b(33) As Byte
> Dim s As String
> Dim i As Integer
>
> For i = 0 To 33
> b(i) = i + 65
> Debug.Print Chr$(b(i));
> Next
> Debug.Print
>
> s = StrConv(b, vbUnicode)
> Debug.Print Left$(s, 32) 'the byte array minus 1 element
>
> End Sub
> '==========
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Please keep all discussions in the groups..
>



Jeff Johnson [MVP: VB]

2005-11-29, 6:55 pm


"Simon Woods" <simonSPAMMENOT.woods@virginNOTMESPAM.net> wrote in message
news:OrYpYHQ9FHA.1188@TK2MSFTNGP12.phx.gbl...

> If I'm honest - I wanted to avoid the Left$ for speed (I know it's
> negligable), and so copy it directly into the string's allocated memory.


How many billions of operations do you plan on doing that you believe speed
will be a factor?


Mike Williams

2005-11-29, 6:55 pm

"Simon Woods" <simonSPAMMENOT.woods@virginNOTMESPAM.net> wrote in message
news:uBoE0uP9FHA.3416@TK2MSFTNGP15.phx.gbl...

> I've a byte array dim'ed 0 to 33 and it's populated with stuff up
> to element 31. I want to copy elements 0-31 into a string variable.
> What's the copy memory call to do this?


If you're using CopyMemory you need to ensure that you have a pointer to the
actual string data and you also need to be aware that strings in VB usually
occupy two bytes per character. Assuming that you want each byte of your
byte array to equate to a character in the string the you're probably better
off using the VB StringConv function instead of CopyMemory. In your specific
case, assuming b1() is your byte array and s1 is your string, you could use
something like:

s1 = Left$(StrConv(b1(), vbUnicode), 32)

Mike




Larry Serflaten

2005-11-29, 6:55 pm


"Simon Woods" <simonSPAMMENOT.woods@virginNOTMESPAM.net> wrote

[color=darkred]
<...>[color=darkred]


You replied:
[color=darkred]
> If I'm honest - I wanted to avoid the Left$ for speed (I know it's
> negligable), and so copy it directly into the string's allocated memory. I
> recognise that the Space$ call may well create temporary strings any way and
> so I'd lose any speed gains over Left$!!!
>
> Just trying to understand ... that's all



What may have been overlooked was that your method did not
address the unicode issue as Ken showed. If your byte array is
a bunch of text then you couldn't use CopyMemory and still
expect text in the string because VB expects a string to contain
Unicode (2 bytes per character). What you have shown, using
CopyMemory, could also be done using VB:

s = LeftB(b, 32)

But again the results would not be printable. It would contain,
byte for byte the values that were in the array, if that is what you
were after. You could then pass that string around, (wherever)
and easily convert it back into an array:

Dim c() as Byte

c = s

So, with an all VB method available, do you still think you have
a need to dip into the API routines?

LFS
David Youngblood

2005-11-29, 9:55 pm

"Simon Woods" <simonSPAMMENOT.woods@virginNOTMESPAM.net> wrote...
> I'm looking to get it into a string variable ... can I not do something like
> this ... I know I can't because this doesn't work !!
>
> l_sRet = Space$(j)
> CopyMemory StrPtr(l_sRet), l_abyOutput(0), j


My understanding is, as a general rule, passing a string ByVal to an API will
pass a pointer to the ANSI equivalent of the string. To pass a pointer to the
Unicode string use ByVal StrPtr(s). So, to accomplish your goal pass the string
ByVal.

CopyMemory ByVal l_sRet, l_abyOutput(0), j

David

An example,

Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" ( _
Destination As Any, _
Source As Any, _
ByVal Length As Long)

Private Sub Command1_Click()
Dim abAnsi() As Byte
Dim abUnicode() As Byte
Dim s As String

s = "Hello World"
abAnsi = StrConv(s, vbFromUnicode)
abUnicode = s
Debug.Print "ANSI "; UBound(abAnsi) + 1; " bytes"
Debug.Print "Unicode "; UBound(abUnicode) + 1; " bytes"

s = Space(25)
CopyMemory ByVal s, abAnsi(0), UBound(abAnsi) + 1
Debug.Print s, "ANSI copy"

s = Space(25)
CopyMemory ByVal StrPtr(s), abUnicode(0), UBound(abUnicode) + 1
Debug.Print s, "Unicode copy"

End Sub



Simon Woods

2005-11-30, 3:55 am


"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:ehQZiVQ9FHA.4084@TK2MSFTNGP10.phx.gbl...
>
> "Simon Woods" <simonSPAMMENOT.woods@virginNOTMESPAM.net> wrote in message
> news:OrYpYHQ9FHA.1188@TK2MSFTNGP12.phx.gbl...
>
>
> How many billions of operations do you plan on doing that you believe
> speed will be a factor?


Jeff

I've got to learn somehow ... half the problem is not knowing what I don't
know and only partially knowing other bits.

I really appreciate everyone's contributions.

Simon


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com