Encoding question

Posted on 2009-12-28
Last Modified: 2012-05-08

I have an encoding problem.
On a Desktop Application (Full Framework) I am reading data from a File using FileStream.
I am reading 10'000 Bytes with fileStream.Read(.....)
Then I am converting it using Encoding.UTF8.GetString to a string.
Then I am adding some value to it and using Encoding.UTF8.GetBytes I am converting it back to a ByteArray which I then send to Silverlight.

There I am reading Bytes from a Stream, but when I then convert it back to UTF8-String, I get something other than what I had before.

Only if I convert it to Base64 on both sides, it is working.

Is it because some File Data cannot be stored in UTF8?


Question by:Ruttensoft
    1 Comment
    LVL 42

    Accepted Solution

    generaly yes.
    To convert a simple UTF-8 coded text into a Base64 string in C# you can use this:
    string result = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(utf8Text));

    The ToBase64String method gets a byte array as a parameter and gives the encoded string back. The result can be stored in a simple string, because the base64 string is a readable ASCII string.

    To decode a UTF-8 text is a slightly more difficult than the encoding.

    byte[] toDecodeByte = Convert.FromBase64String(textToTransform);

    System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
    System.Text.Decoder utf8Decode = encoder.GetDecoder();

    int charCount = utf8Decode.GetCharCount(toDecodeByte, 0, toDecodeByte.Length);

    char[] decodedChar = new char[charCount];
    utf8Decode.GetChars(toDecodeByte, 0, toDecodeByte.Length, decodedChar, 0);
    string result = new String(decodedChar);

    The decoding - like before - can be done with calling a method from the Convert class. The only problem with this, that is gives back a byte array as a result. It should be converted to a UTF-8 string with the System.Text namespace's Decoder class.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    761 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

    10 Experts available now in Live!

    Get 1:1 Help Now