Domanda

Following the ASCII table here asci table the value for 128 is a
However doing the follwoing code in C# doesn't return the euro sign

char expectedEuro = Convert.ToChar(128);

Trying to convert the character into a Char always results in an OverflowException as it is too big for an unsigned byte

Convert.ToByte(`€`);

This is causing problems as our encoding from another system outputs the character when there is a value of 128 but C# outputs an empty character.

Is there an explanation for this and anything I can do to work around this?

È stato utile?

Soluzione

C#/.NET uses Unicode characters, so the sign is 8364.

You can check that with:

int val = (int)'€'; // val will be 8364

This also means you can not represent a char as one byte as most of them need 2 bytes.

If you want to use the extended ASCII table you can use Encoding.Default:

var valStr = Encoding.Default.GetString(new byte[] { 128 }); // valStr = €
var valByte = Encoding.Default.GetBytes("€"); // valByte[0] = 128

Encoding.Default uses the current ANSI code page (see Joe's answer or Jeppe's comment to choose a specific one) and Encoding.ASCII uses the 7-bit ASCII table, so there is no 128 in ASCII.

Altri suggerimenti

ASCII is a 7-bit encoding: the table you linked describes one specific encoding for the Euro character.

If you want to encode the Euro symbol so that it's recognized by the other system, you may want to encode it using a suitable encoding, e.g.:

var v = System.Text.Encoding.GetEncoding(1252).GetBytes("€");
Console.WriteLine(Convert.ToByte(v[0])); // = 128
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top