views:

1155

answers:

3

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?

Thanks.

A: 

Try StringToByteArray here.

Matthew Flaschen
It works for ANSI and UNICODE.
Ubalo
Why would you do all that when you can just use direct assignment and StrConv if you need to go between Unicode/Ascii?
Oorang
+2  A: 

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

Matthew Jones
Both answers works good but this one requires less coding and time.
Ubalo
+3  A: 

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.

Karl E. Peterson
BTW to get your comments highlighted properly in VB code samples, add another apostrophe ' to the end of each code line
MarkJ
Is there a guide to that, somewhere? I found entering the code to be inordinately tedious.
Karl E. Peterson