How to convert back a string that have converted to byte[]

F

FrzzMan

How to convert back a string that have converted to byte[] using
System.Text.Encoding.UTF8.GetBytes()

string StringData = "This is a string";
byte[] ConvertedString = System.Text.Encoding.UTF8.GetBytes(StringData);

Now, how can I convert ConvertedString to "This is a string" string again?
 
E

Erik Frey

FrzzMan said:
How to convert back a string that have converted to byte[] using
System.Text.Encoding.UTF8.GetBytes()

string StringData = "This is a string";
byte[] ConvertedString = System.Text.Encoding.UTF8.GetBytes(StringData);

Now, how can I convert ConvertedString to "This is a string" string again?

System.Text.Encoding.UTF8.GetString

Erik
 
T

Tom Porterfield

FrzzMan said:
How to convert back a string that have converted to byte[] using
System.Text.Encoding.UTF8.GetBytes()

string StringData = "This is a string";
byte[] ConvertedString =
System.Text.Encoding.UTF8.GetBytes(StringData);

Now, how can I convert ConvertedString to "This is a string" string
again?

string System.Text.Encoding.UTF8.GetString(byte[]);

--
Tom Porterfield
MS-MVP MCE
http://support.telop.org

Please post all follow-ups to the newsgroup only.
 
F

FrzzMan

FrzzMan said:
How to convert back a string that have converted to byte[] using
System.Text.Encoding.UTF8.GetBytes()

string StringData = "This is a string";
byte[] ConvertedString = System.Text.Encoding.UTF8.GetBytes(StringData);

Now, how can I convert ConvertedString to "This is a string" string again?

Oops sorry, I didn't know there's GetString method... sry...
 
F

FrzzMan

..NET GetBytes() and GetString() didn't work right with UTF-8 charset.

My following code prove that...

string Data = "UTF-8 string"; // <-- Insert some *long* UTF-8 string
byte[] DataBytes = System.Text.Encoding.UTF8.GetBytes(Data);
string BackConvert = System.Text.Encoding.UTF8.GetString(DataBytes);

System.IO.StreamWriter File = System.IO.File.CreateText("test.txt");
File.Write(Data + "\n" + BackConvert);
File.Close();

Wait a lil, see the attached text file, open it with your browser and
set browser text encoder to UTF-8, you'll see the different...
 
J

Jon Skeet [C# MVP]

FrzzMan said:
.NET GetBytes() and GetString() didn't work right with UTF-8 charset.

My following code prove that...

string Data = "UTF-8 string"; // <-- Insert some *long* UTF-8 string
byte[] DataBytes = System.Text.Encoding.UTF8.GetBytes(Data);
string BackConvert = System.Text.Encoding.UTF8.GetString(DataBytes);

System.IO.StreamWriter File = System.IO.File.CreateText("test.txt");
File.Write(Data + "\n" + BackConvert);
File.Close();

Wait a lil, see the attached text file, open it with your browser and
set browser text encoder to UTF-8, you'll see the different...

Um, I doubt it, to be honest. You'll need to give us a sample string to
show us what you mean.

To check that the strings are the same, you could just use

Console.WriteLine (Data==BackConvert);
 
F

FrzzMan

I doubt it, too... but I did try and it didn't work... in the last post
I forgot to attach the result with sample string, my fault...

In this post I attached the result file of the test, and I tried to
System.Console.WriteLine(Data == BackConvert) before, it give me a False.

You can try it yourself... use the first line in the attached file as
the sample string...
FrzzMan said:
.NET GetBytes() and GetString() didn't work right with UTF-8 charset.

My following code prove that...

string Data = "UTF-8 string"; // <-- Insert some *long* UTF-8 string
byte[] DataBytes = System.Text.Encoding.UTF8.GetBytes(Data);
string BackConvert = System.Text.Encoding.UTF8.GetString(DataBytes);

System.IO.StreamWriter File = System.IO.File.CreateText("test.txt");
File.Write(Data + "\n" + BackConvert);
File.Close();

Wait a lil, see the attached text file, open it with your browser and
set browser text encoder to UTF-8, you'll see the different...


Um, I doubt it, to be honest. You'll need to give us a sample string to
show us what you mean.

To check that the strings are the same, you could just use

Console.WriteLine (Data==BackConvert);


miniLZO -- mini subset of the LZO real-time data compression library Äây là tiếng Việt, nén thá»­ coi có được không nào... Äây là tiếng Việt, nén thá»­ coi có được không nào... Äây là tiếng Việt, nén thá»­ coi có được không nào... Äây là tiếng Việt, nén thá»­ coi có được không nào... Äây là tiếng Việt, nén thá»­ coi có được không nào... Äây là tiếng Việt, nén thá»­ coi có được không nào... Äây là tiếng Việt, nén thá»­ coi có được không nào...
miniLZO -- mini subset of the LZO real-time data compression library Äây là tiếng Việt, nén thá»­ coi có được không nào... Äây là tiếng Việt, nén thá»­ coi có được không nào... Äây là tiếng Việt, nén thá»­ coi có được không nào... Äây là tiếng Việtꀜҥnén thá»­ coi cóê¦Ò¥Æ°á»£c không nào... Äây là tiếng Việt, nén ꀰҥử coi có được không nào... Äây là tiếng ê¶Ò¥á»‡t, nén thá»­ coi có được không nào... Äây là tiếng Việt, nén thá»­ coi có được không ê‚Ò¥o...
 
J

Jon Skeet [C# MVP]

FrzzMan said:
I doubt it, too... but I did try and it didn't work... in the last post
I forgot to attach the result with sample string, my fault...

In this post I attached the result file of the test, and I tried to
System.Console.WriteLine(Data == BackConvert) before, it give me a False.

You can try it yourself... use the first line in the attached file as
the sample string...

You haven't actually really attached a file - just cut and paste it.
Please email me an example and I'll have a look at it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top