简体   繁体   中英

C# converting string to hex and XOR on Hex numbers

Im tring to do an XOR on 2 hex numbers to create a unique hex number

ex. 7F4 ^ 65D which would equal 1A9

I understand how the XOR should work but every time I try to convert the string hex number:

string hex1 = "7F4";
int hexInt = Convert.ToInt32(hex1, 16);

I end up with a number: 2036

How do I keep the integrity of the hex number so I can do an XOR on the 2 hex numbers?

A number is a number, hexadecimal is just the way the number is displayed.

7F4 hexadecimal equals 2036 decimal, and you can perform bitwise operations on it, they will produce the correct output.

Convert the hexadecimal strings to normal integers, perform the XOR on the integers and then format the resulting integer into a hexadecimal string.

string hex1 = "7F4";
string hex2 = "65D";
int dec1 = Convert.ToInt32(hex1, 16);
int dec2 = Convert.ToInt32(hex2, 16);
int result = dec1 ^ dec2;
string hexResult = result.ToString("X");

2036 base 10 IS 0x7F4 base 16.

They are both representations of the same bit pattern:

    0000 0111 1111 0100
       0    7    F    4

/// binary:  

[0*2048 + 1*1024 + 1*512 + 1*256] + [1*128 + 1*64 + 1*32 + 1*16] + [0*8 + 1*4 + 0*2  + 0*1] = 2036 (base 10)

/// hex

7*16*16 + F*16 + 4

= 7*16*16 + 15*16 + 4 = 2036 (base 10)

Do this:

int a = Convert.ToInt32("2036");
int b = Convert.ToInt32("07F4",16);

Console.WriteLine("Same? "+(a==b ? "true" : "false"));

XORing either of those two variables with anything will give you the same result.

I think you're confusing the string representation of a number with the number itself.

2036 IS 7F4 ... they're the same value. Now, when you convert the value using Convert.ToInt32 and then view the result in the debugger you may see the value in base10 - but that doesn't matter. Convert is operating correctly.

Bitwise operations on the value will produce correct results. So for example:

int result = 2036 ^ 1985;  // results in 53 decimal or 0x035 hex

If you need to convert a value back into a Hex string, you can use:

string asHexAgain = hex1.ToString("x");

Try using the NumberStyle specification from the int.Parse method:

int value = int.Parse("7F4",NumberStyles.AllowHexSpecifier);

This gives you the decimal value of the hex number. Now to get it back out as a hex number you can do this:

string hex = value.ToString("X2");

2036 and 0x7f4 are just different representations of the same number. You can still XOR it with another number.

The base a number is represented in doesn't matter. The number 15 base 10 (decimal) and the number 0xF (in hex) are the same number. Just using different bases to represent them. Visual Studio is typically going to show you a number in decimal, as that is what humans are used to.

So XOR'ing the hexInt will work just fine. You can then convert it to a string in hex format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM