Singularity303
Singularity303

Reputation: 69

Concatenation in C #

I have the following piece of code:

byte [] bytes;
string one_simbol;

bytes = BitConverter.GetBytes (n.GetPixel (x, y). R + n.GetPixel (x, y). G + n.GetPixel (x, y). B);
one_simbol = Encoding.GetEncoding (1251). GetString (bytes, 0, bytes.Length);

// Always in one_simbol any one character
// We do the following

full_text + = one_simbol + one_simbol + one_simbol;

No matter how much I add these characters, even if they are different ends up being stored in the variable full_text only 1 character, the first that has been added

Upvotes: 2

Views: 173

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503954

You should absolutely not be using Encoding like this. Your byte array isn't encoded text - so why try to act as if it is? You've got arbitrary binary data, so you should use something like base64 (or perhaps hex) instead to ensure round-tripping:

string text = Convert.ToBase64String(bytes);

That way you'll be able to reverse the data, which I assume is what you want.

Now as to why your concatenation supposedly isn't working: I strongly suspect it is, but that due to control characters etc in your text, you aren't able to see it correctly. Try printing out full_text.Length, and I suspect it will equal one_simbol.Length * 3.

Upvotes: 7

Related Questions