Byte array to String

Im trying to convert a byte array to a string and then back. My issue is I dont seem to be able to encode/decode it right.

Ive got some code:-
        input.Read(buffer, 0, count)
        Dim encoding As New System.Text.UTF8Encoding
        Dim postData As String = "strID=" & fileGUID & "&valBlob=" & UrlEncode(System.Convert.ToBase64String(buffer))

Open in new window


Which gives a result, however if I was to open the file where Im getting the buffer from, it doesnt look anything like the String PostData.

Im not too hot on the subject of encoding and decoding, but what I would like to do is open a file, and see it the same as in Notepad (makes it easier to see where my code is going wrong).

Is it possible to convert a byte array into a string, the same as notepad does?

Thanks in advance
tonelm54Asked:
Who is Participating?
 
Paul_Harris_FusionCommented:
You use the encoding objects in System.Text.Encoding
e.g. assuming we have a byte array called myBytes

at the top of your code module/class
           Imports System.Text.Encoding

Within your function

            Dim myutf As UTF8Encoding = New UTF8Encoding()

            dim myString as string = myutf.GetString(myBytes)
0
 
CodeCruiserCommented:
It all depends on the encoding that you use. You are using UTF8 but there is also ASCII, UTF7, Unicode etc among others.
0
 
tonelm54Author Commented:
I assume notepad will use ASCII? Is there a simple way in VB.net to convert a byte array to a string?  
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
"if I was to open the file where Im getting the buffer from"

Why don't you read the file straight into a string with a StreamReader instead of going through a byte array?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.