Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a simple string to Byte Array in VBA?

I need to convert a simple string in a Byte array using excel VBA. Then this byte array is used as the request's body.

How can I do that?

like image 204
Ubalo Avatar asked Jun 15 '09 22:06

Ubalo


2 Answers

Matthew answered how to convert to ANSI, but if you wanted the resulting byte array to still represent the original Unicode string, you'd simply assign it directly:

Public Sub Main()
   Dim b() As Byte
   Dim s As String
   s = "Whatever"
   b = s  'Assign Unicode string to bytes.'
   s = b  'Works in reverse, too!'
   Debug.Print s
End Sub

That's all there is to it. You end up with a 16-element Byte array, each successive pair describing one Unicode character.

like image 138
Karl E. Peterson Avatar answered Sep 20 '22 08:09

Karl E. Peterson


If you only need ANSI characters, you can use the StrConv() function as is done here.

like image 33
Matthew Jones Avatar answered Sep 18 '22 08:09

Matthew Jones