Solved

Convert Byte Array to String (again)

Posted on 2001-06-13
7
232 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

713 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