Solved

Convert Byte Array to String (again)

Posted on 2001-06-13
7
228 Views
Last Modified: 2010-08-05

 I have this situation:

 Public ByteArray() As Byte
 Public MyString As String

 I need 2 functions:
 - Convert ByteArray() to MyString
 - Convert MyString to ByteArray()

 I want to convert using the CopyMemory API,
 and not a solution with a loop or something.

 PLEASE HELP! :)
 This is drivin' me crazy since a few days because the
 answer on my previous question doesn't seem to work
 so well after all... :(

 Kind regards,


 Robin
0
Comment
Question by:dwalsarie
7 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 100 total points
ID: 6186603
Bytes to string
MyString = StrConv(ByteArray, vbUnicode)

string to bytes
ByteArray = StrConv(MyString, vbFromUnicode)
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6186623
If the Byte array is supposed to be double width (same as unicode string) then just use the assignment.

MyString = ByteArray

ByteArray = MyString

Remember to assign to the byte array, it must be a dynamic array.
0
 
LVL 3

Expert Comment

by:wpsjr1
ID: 6186711
Public Function ByteArrayToString(ByteArray() As Byte) As String
  On Error GoTo done ' in case the Byte Array is not initialized
 
  Dim lBytes As Long

  If LBound(ByteArray) > 0 Then Exit Sub ' lBound MUST be 0
  lBytes = UBound(ByteArray) + 1
  ByteArrayToString = Space$(lBytes)
 
  RtlMoveMemory ByVal ByteArrayToString, ByteArray(0), lBytes
  Exit Sub
done:
  Debug.Print "Error!  You did not initialize the Byte Array first."
End Sub

Public Sub StringToByteArray(ByVal StringIn As String, ByteArray() As Byte)
    Dim lBytes As Long
   
    If Len(StringIn) = 0 Then Exit Sub
    lBytes = Len(StringIn)
    ReDim ByteArray(lBytes - 1)
   
    RtlMoveMemory ByteArray(0), ByVal StringIn, lBytes
End Sub

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:wpsjr1
ID: 6186728
hrm, this is slightly more efficient:

Public Sub StringToByteArray(ByVal StringIn As String, ByteArray() As Byte)
    Dim lBytes As Long
   
    lBytes = Len(StringIn)
    If lBytes = 0 Then Exit Sub
    ReDim ByteArray(lBytes - 1)
   
    RtlMoveMemory ByteArray(0), ByVal StringIn, lBytes
End Sub

Heres the declare too:

Public Declare Sub RtlMoveMemory Lib "kernel32.dll" (dest As Any, src As Any, ByVal cb As Long)
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6187508
hearing...
0
 
LVL 2

Author Comment

by:dwalsarie
ID: 6189836
great! I didn't even knew this function StrConv *shame on me* .. It works excellent for me .. yipeeeh! :D

Now I can finally finish my project I worked on for so long time...
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6190865
Glad to help.  Thanks for the points.  :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to skip loop 6 55
MsgBox 2 47
Sending a email via excel using vba 6 72
VBA Shell can't Find Word document 11 76
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

895 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

13 Experts available now in Live!

Get 1:1 Help Now