By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,814 Members | 1,671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,814 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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.