GetByte adding an extra byte?

S

Steven Blair

string USMesg;

USMesg = "¬Credits¬Remaining¬";

byte[] bArray = Encoding.UTF8.GetBytes(USMesg);

After I execute the code, the first two bytes are:

[0] = 0xC2
[1] = 0xAC //This is the character I was expecting

Why has an extra byte been inserted after each ¬?

I tried using Encoding.ASCII.GetBytes() but that translates my ¬ to a
0x3F (Question mark ?)

Anyone any idea whats happened and how I can get round this problem?

Regards,

Steven
 
U

UL-Tomten

USMesg = "¬Credits¬Remaining¬";
byte[] bArray = Encoding.UTF8.GetBytes(USMesg);
[1] = 0xAC //This is the character I was expecting
Why has an extra byte been inserted after each ¬?

This is how UTF-8 works. I assume that when/if you review the UTF-8
specifications, you will find that the character "¬" is to be
represented as 0xC2AC in this particular scenario.
I tried using Encoding.ASCII.GetBytes()

ASCII and UTF-8 are not interchangeable.
Anyone any idea whats happened and how I can get round this problem?

What is happening is that characters are being converted to bytes,
using the character encoding you specify. The process of going
between actual characters and bits is very complex.

Perhaps what you want is Encoding.Default.GetBytes()? This will use
the system default ANSI codepage (in your case Windows-1252, which
internally means ISO-8859-1 (aka "Latin-1" or "Western European")).
This might encode "¬" as 0xAC, or it might not.

However, if you want to write predictable code, you must agree with
whoever will read the bytes back upon which encoding to use.
Otherwise, when reading 0xC2AC back into a string, the reader might
get a tiny picture of a tiny goat instead of the "¬".
 
J

Jon Skeet [C# MVP]

string USMesg;

USMesg = "¬Credits¬Remaining¬";

byte[] bArray = Encoding.UTF8.GetBytes(USMesg);

After I execute the code, the first two bytes are:

[0] = 0xC2
[1] = 0xAC //This is the character I was expecting

Why has an extra byte been inserted after each ¬?

I tried using Encoding.ASCII.GetBytes() but that translates my ¬ to a
0x3F (Question mark ?)

Anyone any idea whats happened and how I can get round this problem?

It sounds like you should read up on the UTF-8 format. Using
Encoding.Default may well give you what you want, but UTF-8 is
generally a better format these days.

See http://pobox.com/~skeet/csharp/unicode.html and the referenced
links there.

Jon
 
U

UL-Tomten

Unicode characters use two bytes.

O RLY?

"Unicode" can be anything from UTF7 to UTF32. In this case, it was
UTF8. Each use different numbers of bits to represent characters.
Also, UTF8 uses anywhere between 1 and 4 (IIRC) bytes to represent a
character.

Perhaps you were thinking of "wide characters" from old-school Win32
programming?
 
J

Jon Skeet [C# MVP]

Unicode characters use two bytes.

True, but irrelevant in this case - the important thing is that the
UTF-8 encoded version of the relevant character takes two bytes.
(Other characters can take 1 or 3.)

Jon
 

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

Similar Threads


Top