Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# converting string to hex and XOR on Hex numbers

Tags:

c#

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?

like image 510
Scott's Oasys Avatar asked Jan 19 '26 14:01

Scott's Oasys


2 Answers

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.

like image 78
C.Evenhuis Avatar answered Jan 22 '26 03:01

C.Evenhuis


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");
like image 22
Deniz Dogan Avatar answered Jan 22 '26 03:01

Deniz Dogan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!