473,765 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,3 3,44}

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

and then back to the byte array?

Thanks.

--
mo*******@nospa m.nospam
Nov 21 '05 #1
6 53723
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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).ToStri ng()
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.ToBase6 4String(mByte)
MsgBox(ss)
Dim mb() As Byte = Convert.FromBas e64String(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*******@nosp am.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.En coding.<encodin g>.{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.Enc oding.GetBytes &
Encoding.GetStr ing 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(B yVal 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.Str ingBuilder(byte s.Length * 4)
For Each b As Byte In bytes
sb.Append(b.ToS tring(format))
sb.Append(","c)
Next
sb.Length -= 1
Return sb.ToString()
End Function

Private Shared Function StringToArray(B yVal s As String, Optional ByVal
style As System.Globaliz ation.NumberSty les = Nothing) As Byte()
If s.Length = 0 Then Return New Byte() {}
Dim values() As String = s.Split(","c)
Dim bytes(values.Le ngth - 1) As Byte
For index As Integer = 0 To values.Length - 1
bytes(index) = Byte.Parse(valu es(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(m Byte, "x2")
myStr = ArrayToString(m Byte)

Debug.WriteLine (myStr)
mByte = Nothing
'mByte = StringToArray(m yStr,
Globalization.N umberStyles.All owHexSpecifier)
mByte = StringToArray(m yStr)

End Sub

Hope this helps
Jay

"moondaddy" <mo*******@nosp am.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP14.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,3 3,44}
|
| Now how do I convert it to:
| dim myStr as string = "11,22,33,4 4"
|
| and then back to the byte array?
|
| Thanks.
|
|
|
| --
| mo*******@nospa m.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*******@nospa m.nospam
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:%2******** ********@TK2MSF TNGP15.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.Enc oding.GetBytes &
Encoding.GetStr ing 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(B yVal 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.Str ingBuilder(byte s.Length * 4)
For Each b As Byte In bytes
sb.Append(b.ToS tring(format))
sb.Append(","c)
Next
sb.Length -= 1
Return sb.ToString()
End Function

Private Shared Function StringToArray(B yVal s As String, Optional ByVal
style As System.Globaliz ation.NumberSty les = Nothing) As Byte()
If s.Length = 0 Then Return New Byte() {}
Dim values() As String = s.Split(","c)
Dim bytes(values.Le ngth - 1) As Byte
For index As Integer = 0 To values.Length - 1
bytes(index) = Byte.Parse(valu es(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(m Byte, "x2")
myStr = ArrayToString(m Byte)

Debug.WriteLine (myStr)
mByte = Nothing
'mByte = StringToArray(m yStr,
Globalization.N umberStyles.All owHexSpecifier)
mByte = StringToArray(m yStr)

End Sub

Hope this helps
Jay

"moondaddy" <mo*******@nosp am.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP14.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,3 3,44}
|
| Now how do I convert it to:
| dim myStr as string = "11,22,33,4 4"
|
| and then back to the byte array?
|
| Thanks.
|
|
|
| --
| mo*******@nospa m.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
8710
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
8792
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 the original str -"1234" from m_str?
2
2963
by: Soja | last post by:
Hello, how can i convert a String to string format.
1
2772
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
3397
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() (BinaryWriter.Write) from the string (String.ToCharArray), then use Stream.ToArray to convert everything to byte(). It works most of the time, but it happens that an error tells me something like "Additional information: Caractère de substitution faible trouvé...
4
4024
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 to strings. How do I convert this raw data from one format to another? I don't think system.text.encoding is useful here as the data is not in any particular encoding format, it's just raw data and it needs to stay
5
5194
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 table and I get the error: ORA-01858: a non-numeric character found where a digit was expected. Any help would be greatly appreciated try { java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("DD/MM/yy");
2
2565
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 strResult.Remove(0) and all it did was delete characters, not remove the string. I can create a string the correct size using "new string " and then copy but it seems there should be an easier way. I cant seem to make VS 2008 get me good help on "string"...
6
6489
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 strcmp() function #include<stdio.h> #include<string.h> int main(){ int i,n;
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9951
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8831
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.