473,396 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 53647
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: MrNobody | last post by:
is there a quick way to convert string into byte array or do I have to iterate through and convert each character into a byte?
3
by: gg | last post by:
Hi, Dear All, Please see the following code: SHA1 sha1 = SHA1.Create(); string str="1234"; byte m_str = sha1.ComputeHash( Encoding.Unicode.GetBytes( str) ); ..... I want to know how to get...
2
by: Soja | last post by:
Hello, how can i convert a String to string format.
1
by: DaveF | last post by:
I need to do some calculations with lat long. Does anyone have an example how to work with lat long so I don't loose values -- David
4
by: ThunderMusic | last post by:
Hi, I have to go from Byte() to String, do some processing then reconvert the String to byte() but using ascii format, not unicode. I currently use a stream to write the char()...
4
by: movieknight | last post by:
Hi, I have an application that stores raw .wav files (and also jpgs/bitmaps) within strings, and I need to sometimes convert these strings to byte arrays, and sometimes go from byte arrays back...
5
by: pmagee | last post by:
I am trying to convert a date from string format into the SQL date format. This seems to work fine as it prints out as 1999-01-01 - see code below. I then try to insert this as a variable into my...
2
by: Beemer Biker | last post by:
I wanted to truncate a string array by removing the last item. "strResult.length = n-1" does not work as the length is read only. I tried strResult = null and just got a null. Also tried...
6
by: cthoes | last post by:
following program does the display of student details but i want to search for the particular student name from the added list of student. so it would be great any one can help me without using...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.