Solved

Convert Byte Array to String (again)

Posted on 2001-06-13
7
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: 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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 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…
Suggested Courses
Course of the Month2 days, 18 hours left to enroll

622 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