Solved

Convert Byte Array to String (again)

Posted on 2001-06-13
7
222 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

707 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

12 Experts available now in Live!

Get 1:1 Help Now