0

I want to add two hexadecimal numbers in hex mode and result should also be hexadecimal. But the problem is I have a string "20010000" and int 0x400050, how should I add those to receive 0x20410050 ?

Ive tried int.Parse with various hex options but it always results as decimal addition.

3
  • Can you share some variable declarations? Is it string foo = "20010000"; int bar = 0x400050;? If so, I think your question is just "How do I parse a string as a hexadecimal number?" Right? Commented Jul 24, 2016 at 22:22
  • how to add bar to foo like both were hex and receive hex number. Its more like "How to parse string as hex but leave its value", when I were searching for solution, only "how to parse decimal string to hex int" pops up. Commented Jul 24, 2016 at 22:25
  • I'm doing a lot of guessing as to what you actually want, but see if my answer helps. Commented Jul 24, 2016 at 22:29

1 Answer 1

1

Is this what you're looking for?

string aString = "20010000";
int a = Convert.ToInt32(aString, 16); // should be 536936448
int b = 0x400050; // should be 4194384

int sum = a + b; // should be 541130832
string sumString = sum.ToString("X"); // should be "20410050"
string sumStringWithPrefix = "0x" + subString; // should be "0x20410050"
Sign up to request clarification or add additional context in comments.

1 Comment

Thats what Ive been looking for, was trying something simillar before but for some reason it doesnt works. Thanks for your reply

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.