Solved

Convert Byte Array to String (again)

Posted on 2001-06-13
7
230 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

776 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