473,699 Members | 2,933 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 53714
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
8709
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
8787
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
2959
by: Soja | last post by:
Hello, how can i convert a String to string format.
1
2766
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
3394
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
4020
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
5189
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
2562
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
6488
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
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9185
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9050
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8935
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
8893
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7773
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...
0
5879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
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.