[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

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
0
tonelm54
Asked:
tonelm54
1 Solution
 
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
 
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
 
Jacques Bourgeois (James Burger)Commented:
"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

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now