471,621 Members | 1,328 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,621 software developers and data experts.

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

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.

--
mo*******@nospam.nospam
Nov 21 '05 #1
6 53049
Moondaddy,

Have a look at the encode functions GetString and GetBytes.

http://msdn.microsoft.com/library/de...tringtopic.asp

http://msdn.microsoft.com/library/de...ytestopic2.asp

I hope this helps,

Cor
Nov 21 '05 #2
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.

Nov 21 '05 #3
"moondaddy" <mo*******@nospam.nospam> schrieb:
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}'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #4
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

"moondaddy" <mo*******@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
| 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.
|
|
|
| --
| mo*******@nospam.nospam
|
|
Nov 21 '05 #5
Thanks. I found i need to use this one since the array comes from a
differenct type of encoding used in an encryption function and I found that
I use the other methods above it actually chances the data so it can't be
restored.

Thanks to ALL.

--
mo*******@nospam.nospam
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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

"moondaddy" <mo*******@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
| 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.
|
|
|
| --
| mo*******@nospam.nospam
|
|

Nov 21 '05 #6
Hi

Thanks for your confirm.

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.

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by MrNobody | last post: by
2 posts views Thread by Soja | last post: by
1 post views Thread by DaveF | last post: by
4 posts views Thread by ThunderMusic | last post: by
2 posts views Thread by Beemer Biker | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.