views:

54

answers:

2

In short:

For this code:

Encoding.ASCII.GetBytes("‚")

I want the output to be 130, but this gives me 63.

I am typing the string using Alt+0130.

+2  A: 

On my setup:

Encoding.ASCII.GetBytes("‚"); // 63
Encoding.Default.GetBytes("‚"); // 130

Of course 'default' could very well be environment-dependent...

David Hedlund
+2  A: 

When you try to encode the string using the ASCII encoding, it will be converted to a question mark as there is no such character in the ASCII character set. The character code for the question mark is 63.

You need to use an encoding that supports the character, to get it's actual character code.

One option is to use the Encoding.Default property to get the encoding for the system codepage, as David suggested. However as the system codepage can differ, it's not guaranteed to give the same result on all computers.

The unicode character code is 8218, which you can get by simply converting the character to an int:

int characterCode = (int)'‚';

As this is not depending on any system settings, you should consider if you can use that instead of the encoded byte value.

Guffa
Actually, the code ',' is stored is stored in the database and it is a legacy system. So pretty much nothing can be done about it. I DO want '130' for the ',' stored in the database.
nullDev