[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Encoding question

Posted on 2009-12-28
1
Medium Priority
?
204 Views
Last Modified: 2012-05-08
Hello

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?

Thanks

James
0
Comment
Question by:Ruttensoft
1 Comment
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 total points
ID: 26130476
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.

Decoding
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2
Suggested Courses

872 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