3

probably mine is a silly question, but I'm having troubles converting a char value into an int and convert back.

The problem is that I'm trying to decrypt a char value retrieved by an Access DB.

Here is my code

char chrVal = 'M';
int intVal = (int)chrVal; // Output 77 'M'
// Now trying to encrypt using XOR
int encIntVal = intVal ^ 203; // Output 134 '†'
// Convert back
char correct = (char)(encIntVal ^ 203); // Output 'M' - CORRECT
char wrong = (char)('†' ^ 203); // Output WRONG value

The fact is that when I use the int resulting value from the encrypt XOR, I get correct result ('M'). Instead, when I use the char result from the encrypt XOR (that is what I have in the DB), I get wrong result (unreadable character).

I tried to use different encodings but I can't figure out where is the problem.

Any suggestion?

UPDATE

I found that probably the problem is with ADO.NET OleDbDataReader, because (int)Convert.ToChar(dr["Sex"]) gives me 8224 instead of 134, but I can't find a solution yet.

SOLVED

The character '†' is in the Windows 1252 code page. So I get a byte[] with the correct encoding.

byte[] byteVal = Encoding.GetEncoding(1252).GetBytes(dr["Sex"])
char correct = (char)(byteVal[0] ^ 203); // Output 'M'

Thanks

4

2 回答 2

3

'†' 字符可以是多个 Unicode 值而不是一个的前字符。

我可以创建一个字体,其中“A”不仅仅适用于 65 ASCII 值,而是任何值,或者我可以创建一个所有字符都是“A”的字体。

就像在你的情况下'†'可以是你所说的 134 和 Oded 提到的 8224。

Give more emphasis on ASCII/Unicode values and not on what that value when converted to character looks like.

于 2012-05-28T13:05:33.680 回答
1

您的问题是'†'8224,而不是 134。(@Oded 首先提到了这一点)您可以改用'\u0086'134。

于 2012-05-28T13:07:28.703 回答