Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
2.31/5 (3 votes)
See more:
Hi guys,

I convert an array of Byte to String by this method:
my array of Byte is myArray that i get it form another place,
So, i do not know the code page that used to get it

C#
StringBuilder StringPlus = new StringBuilder();
for (int i = 0; i < myArray.Length; i++)
{
    StringPlus.Append(myArray[i].ToString());
}


I want to convert from String to Byte whereas the resulted Array equal to my first
Important note:
I do not know the Without knowing the code page that used to get the original array myArray


i want that:
convert from Byte array to string then to Byte array again
Not from string to Byte array then to string
Posted
Updated 22-Jun-11 14:48pm
v4
Comments
madper 19-Jun-11 20:53pm    
I was try all the method mentioned here to convert a bytes array into string. Everything goes nice until i have a byte value that will be converted into char '\0' that means End Of String. So when i try to print my string result, the string was truncated there at that point.

Can anyone give a solution for this?

Thanks before.
MrLonely_2 22-Jun-11 20:51pm    
Thanks man ,
can u give me the code to try solve it with u

String can be converted do byte array in few different ways, due to the following fact: .NET supports Unicode, and Unicode standardizes several difference encodings called UTFs. They have different lengths of byte representation but are equivalent in that sense that when a string is encoded, it can be coded back to the string, but if the string is encoded with one UTF and decoded in assumption of different UTF, if can be screwed up.

Also, .NET supports non-Unicode encodings, but they are not valid in general case (will be valid only if a limited sub-set of Unicode code point is used in an actual string, such as ASCII). Internally, .NET supports UTF-16, but for stream representation UTF-8 is usually used. It is also a standard-de-facto for Internet.

Not surprisingly, serialization of string into array of byte and deserialization is supported by the class System.Text.Encoding, which is an abstract class; its derived classes support concrete encodings: ASCIIEncoding and four UTFs (System.Text.UnicodeEncoding supports UTF-16).

See http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx[^].

For serialization to array of bytes use System.Text.Encoding.GetBytes. For the inverse operation use System.Text.Encoding.GetChars. This function returns array of characters, so to get a string, use a string constructor System.String(char[]).

See http://unicode.org/[^], http://unicode.org/faq/utf_bom.html[^].

[EDIT]
Example:

C#
string myString = //... some string

System.Text.Encoding encoding = System.Text.Encoding.UTF8; //or some other, but prefer some UTF is Unicode is used
byte[] bytes = encoding.GetBytes(myString);

//next lines are written in response to a follow-up questions:

myString = new string(encoding.GetChars(bytes));
byte[] bytes = encoding.GetBytes(myString);
myString = new string(encoding.GetChars(bytes));
byte[] bytes = encoding.GetBytes(myString);

//how many times shall I repeat it to show there is a round-trip? :-)


—SA
 
Share this answer
 
v3
Comments
MrLonely_2 14-Jun-11 23:15pm    
Thank you,... but can u give me an Example please.
Sergey Alexandrovich Kryukov 17-Jun-11 23:28pm    
OK, see the update (after [EDIT]).
If it's clear now, please formally accept the answer (green button).
If not yet, please ask your questions.
--SA
MrLonely_2 22-Jun-11 20:47pm    
Thanks, but i want that:
convert from Byte array to string then to Byte array again....... not from string to Byte array then to string
Sergey Alexandrovich Kryukov 22-Jun-11 23:23pm    
Is is so hard to understand this is the same?
OK. Look at the updated code. Don't rush. Can you see it now?
Can you read the referenced MSDN page on encoding and understand ***yourself*** how it works?

Any questions?
--SA
MrLonely_2 24-Jun-11 15:34pm    
Ok thanks man
//string to byte array
string theMessage = "This is a message!";
byte[] bytes = Encoding.ASCII.GetBytes(theMessage);
//byte array to string
string theMessage2 = Encoding.ASCII.GetString(bytes);
Console.WriteLine(theMessage2);
 
Share this answer
 
C#
//string to byte array
String str = "Hello";
byte[] btarr = ASCIIEncoding.ASCII.GetBytes(str);

//byte array to string
StringBuilder StringPlus = new StringBuilder();
for (int i = 0; i < btarr.Length; i++)
{
   StringPlus.Append(Convert.ToChar(btarr[i]));
}
 
Share this answer
 
Comments
MrLonely_2 14-Jun-11 23:16pm    
Thanks man but the resulted Array not matches the original arrray
try this.

C#
//Test string
string str = "Test";
//Array containing the bytes from the teststring.
byte[] arr = System.Text.Encoding.Default.GetBytes(str);
//Convert byte array back into a string.
string str2 = System.Text.Encoding.Default.GetString(arr);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900