Arctic
Arctic

Reputation: 827

Why doesn't my c# code recognize copyright symbol?

byte[] newBytes = new Byte[] { 169 };
string string1 = System.Text.Encoding.UTF8.GetString(newBytes, 0, newBytes.Length);

In the above program, I expected string1 to have the value of copyright symbol ©.

But I get some other value (possibly some junk) as shown below

enter image description here

Where did I go wrong?

Upvotes: 3

Views: 2066

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149058

UTF8 requires multiple bytes to encode character points greater than 127. If you run the reverse, you'll see what it expects:

System.Text.Encoding.UTF8.GetBytes("©"); // { 194, 169 }

Try this:

byte[] newBytes = new Byte[] { 194, 169 };
string string1 = System.Text.Encoding.UTF8.GetString(newBytes, 0, newBytes.Length);

If you absolutely have to use that original byte array, you'll need to pick a different encoding. For example, the Windows-1252 encoding uses a single byte to encode the copyright symbol:

byte[] newBytes = new Byte[] { 169 };
var encoding = Encoding.GetEncoding(1252);
string string1 = encoding.GetString(newBytes, 0, newBytes.Length); // "©"

Upvotes: 9

Related Questions