Byte array to String

Posted on 2011-10-30
Last Modified: 2012-05-12
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
Question by:tonelm54
    LVL 83

    Expert Comment

    It all depends on the encoding that you use. You are using UTF8 but there is also ASCII, UTF7, Unicode etc among others.

    Author Comment

    I assume notepad will use ASCII? Is there a simple way in to convert a byte array to a string?  
    LVL 12

    Accepted Solution

    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)
    LVL 40

    Expert Comment

    by:Jacques Bourgeois (James Burger)
    "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?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now