String to Byte Array convertion?!

me_again

Member
Joined
Mar 26, 2003
Messages
14
Location
Boksburg, South Africa
(RESOLVED) String to Byte Array convertion?!

Hi everyone,

String to Byte Array convertion?!

Ive searched the forum for similar posts but couldnt seem to find any that satisfied my question.

In VB6 you could convert a String to a Byte array using
Code:
Dim buffer
Dim sStr As String
   String to Byte Array
   buffer = StrConv(sStr, vbFromUnicode)
   and back to string
   sStr = StrConv(buffer, vbUnicode)

How on earth do you do the same type of thing in VB.NET?

I need to do this for an Overloaded function - one function excepts a String parameter and the next function excepts a Byte array parameter. I then call the latter from the first, and want to pass the converted string into the Byte array parameter.

Thanks in advance.
 
Last edited by a moderator:
(RESOLVED) String to Byte Array convertion?!

Its amazing what can be achieved when the old grey matter is put into action.

I followed to old trusted method : Wrote what I wanted in VB6 and had .NETs upgrade wizard handle the rest.

This is what i got:

VB6 CODE:
Code:
Public Sub Main()
   Dim s As String
   Dim b() As Byte
   
   s = "Some String"
   
   b = StrConv(s, vbFromUnicode)
   s = vbNullString
   s = StrConv(b, vbUnicode)
End Sub

VB.NET CODE:
Code:
Public Sub Main()
   Dim s As String
   Dim b() As Byte

   s = "Some String"

   b = System.Text.UnicodeEncoding.Unicode.GetBytes(s)
   s = vbNullString
   s = System.Text.UnicodeEncoding.Unicode.GetString(b)
End Sub

Problem solved!
 
Back
Top