Answered by:
converting byte array to string

Question
-
First of all I can't take credit for this code.... i've copied it from another forum post...
Code SnippetFor Each p As System.Drawing.Imaging.PropertyItem In b.PropertyItems
System.Text.RegularExpressions.Regex.Replace(strValue,
"[^\w\s\p{P}]", "")strTemp += strValue + ControlChars.CrLf
basically, when i traced it through... it's converting the values fine... it's just that some of them are being translated to "Canon" and others "Canon..... so without the " at the end... i'm presuming this is the problem.. but I can't work out why?
Can anyone help???
please ignore the pointlessness of the program, it's to be used as part of a much larger project and will be slightly changed to make it useful.
Kind Regards,
Martin
Saturday, September 15, 2007 4:32 PM
Answers
-
Hi Martin try this and
please read my code comments.
Add just
1 button
to a FORM to try this please.>>
Code SnippetPublic
Class Form1'The following highlighted line should be one line of code in your code window.>>>>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Set up a BYTE array.
Dim myByteArray(10) As Byte 'Set up an OBJECT variable to hold the ARRAY and 'assign to it the entire ARRAY "myByteArray". Dim anArray As Array = myByteArray 'The following values are taken from the ASCII 'character set seen on this web site.>> 'http://www.asciitable.com/myByteArray(0) =
CByte("72") 'CBYTE = Convert to BYTE.myByteArray(1) =
CByte("105")myByteArray(2) =
CByte("32")myByteArray(3) =
CByte("116")myByteArray(4) =
CByte("104")myByteArray(5) =
CByte("101")myByteArray(6) =
CByte("114")myByteArray(7) =
CByte("101")myByteArray(8) =
CByte("33")myByteArray(9) =
CByte("33") 'Create a STRING to hold the final message. Dim msg As String = "" 'Put the ARRAY into the string called "msg" 'using the FUNCTION "convertByteArrayToString"msg = convertByteArrayToString(myByteArray)
'Show the message.MessageBox.Show(msg)
End SubreturnString &= Chr(
CInt(anArray(index))) Next 'Return the result of the FOR NEXT loop. Return returnString End FunctionEnd
ClassRegards,
S_DS
P.S. I could write a FUNCTION to do the reverse as well if you need it?
That is convert a STRING back into a BYTE array.
Sunday, September 16, 2007 5:58 PM -
Minor correction (only 'cause this is a common mistake when using VB); this line of code:
Code SnippetDim myByteArray(10) As Byte
will actually create an array with 11 items in it. Unlike C/C#/C++/Java, the value is the upper bound, not the number of items in the array.
My main point is that, for (almost) all string to/from byte array manipulations, you really want to use the System.Text.Encoding classes. In this case, since you have an ASCII encoded byte array, you'd use System.Text.Encoding.ASCII.GetString(myByteArray).
And the framework already contains the function to convert the string into a byte array - System.Text.Encoding.ASCII.GetBytes
Best regards,
Johan Stenberg
Monday, September 17, 2007 7:10 PMModerator
All replies
-
Hi Martin try this and
please read my code comments.
Add just
1 button
to a FORM to try this please.>>
Code SnippetPublic
Class Form1'The following highlighted line should be one line of code in your code window.>>>>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Set up a BYTE array.
Dim myByteArray(10) As Byte 'Set up an OBJECT variable to hold the ARRAY and 'assign to it the entire ARRAY "myByteArray". Dim anArray As Array = myByteArray 'The following values are taken from the ASCII 'character set seen on this web site.>> 'http://www.asciitable.com/myByteArray(0) =
CByte("72") 'CBYTE = Convert to BYTE.myByteArray(1) =
CByte("105")myByteArray(2) =
CByte("32")myByteArray(3) =
CByte("116")myByteArray(4) =
CByte("104")myByteArray(5) =
CByte("101")myByteArray(6) =
CByte("114")myByteArray(7) =
CByte("101")myByteArray(8) =
CByte("33")myByteArray(9) =
CByte("33") 'Create a STRING to hold the final message. Dim msg As String = "" 'Put the ARRAY into the string called "msg" 'using the FUNCTION "convertByteArrayToString"msg = convertByteArrayToString(myByteArray)
'Show the message.MessageBox.Show(msg)
End SubreturnString &= Chr(
CInt(anArray(index))) Next 'Return the result of the FOR NEXT loop. Return returnString End FunctionEnd
ClassRegards,
S_DS
P.S. I could write a FUNCTION to do the reverse as well if you need it?
That is convert a STRING back into a BYTE array.
Sunday, September 16, 2007 5:58 PM -
Minor correction (only 'cause this is a common mistake when using VB); this line of code:
Code SnippetDim myByteArray(10) As Byte
will actually create an array with 11 items in it. Unlike C/C#/C++/Java, the value is the upper bound, not the number of items in the array.
My main point is that, for (almost) all string to/from byte array manipulations, you really want to use the System.Text.Encoding classes. In this case, since you have an ASCII encoded byte array, you'd use System.Text.Encoding.ASCII.GetString(myByteArray).
And the framework already contains the function to convert the string into a byte array - System.Text.Encoding.ASCII.GetBytes
Best regards,
Johan Stenberg
Monday, September 17, 2007 7:10 PMModerator