How to convert byte() to string, and from string back to byte()

M

moondaddy

I'm writing an app in vb.net 1.1 and need to convert a byte array into a
string, and then from a string back to a byte array.

for example

Private mByte() as New Byte(4){11,22,33,44}

Now how do I convert it to:
dim myStr as string = "11,22,33,44"

and then back to the byte array?

Thanks.
 
P

Peter Huang [MSFT]

Hi

I thin you may need to use the code similar with below.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim mByte() As Byte = New Byte(3) {11, 22, 33, 44}
Dim strs(3) As String
For i As Integer = 0 To 3
strs(i) = mByte(i).ToString()
Next
For Each s As String In strs
Debug.WriteLine(s)
Next
Dim b As Byte = Convert.ToByte(strs(0))
End Sub

If you wants to transfer the data as a string type, I think you may try to
use the Base64encoding.
Dim ss As String = Convert.ToBase64String(mByte)
MsgBox(ss)
Dim mb() As Byte = Convert.FromBase64String(ss)


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

Herfried K. Wagner [MVP]

moondaddy said:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a
string, and then from a string back to a byte array.

'System.Text.Encoding.<encoding>.{GetBytes, GetString}'.
 
J

Jay B. Harlow [MVP - Outlook]

moondaddy,
In addition to the other comments:

Does your byte array contain Char codes, such as ASCII, ANSI, or Unicode
characters? In which case you can use System.Text.Encoding.GetBytes &
Encoding.GetString to convert to & from a string (as Cor & Herfried
suggested).

Of if you simply want a comma delimited list of numbers from the Byte Array
I would create a couple of helper functions (As Peter suggested).

Something like:

Private Shared Function ArrayToString(ByVal bytes() As Byte, Optional
ByVal format As String = Nothing) As String
If bytes.Length = 0 Then Return String.Empty
Dim sb As New System.Text.StringBuilder(bytes.Length * 4)
For Each b As Byte In bytes
sb.Append(b.ToString(format))
sb.Append(","c)
Next
sb.Length -= 1
Return sb.ToString()
End Function

Private Shared Function StringToArray(ByVal s As String, Optional ByVal
style As System.Globalization.NumberStyles = Nothing) As Byte()
If s.Length = 0 Then Return New Byte() {}
Dim values() As String = s.Split(","c)
Dim bytes(values.Length - 1) As Byte
For index As Integer = 0 To values.Length - 1
bytes(index) = Byte.Parse(values(index), style)
Next
Return bytes
End Function

Public Shared Sub Main()

Dim mByte() As Byte = New Byte() {11, 22, 33, 44}
Dim myStr As String

'myStr = ArrayToString(mByte, "x2")
myStr = ArrayToString(mByte)

Debug.WriteLine(myStr)
mByte = Nothing
'mByte = StringToArray(myStr,
Globalization.NumberStyles.AllowHexSpecifier)
mByte = StringToArray(myStr)

End Sub

Hope this helps
Jay

| I'm writing an app in vb.net 1.1 and need to convert a byte array into a
| string, and then from a string back to a byte array.
|
| for example
|
| Private mByte() as New Byte(4){11,22,33,44}
|
| Now how do I convert it to:
| dim myStr as string = "11,22,33,44"
|
| and then back to the byte array?
|
| Thanks.
|
|
|
| --
| (e-mail address removed)
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top