2

Now I know that converting a int to hex is simple but I have an issue here. I have an int that I want to convert it to hex and then add another hex to it. The simple solution is int.Tostring("X") but after my int is turned to hex, it is also turned to string so I can't add anything to it until it is turned back to int again.

So my question is; is there a way to turn a int to hex and avoid having turned it to string as well. I mean a quick way such as int.Tostring("X") but without the int being turned to string.

John P.
  • 1,199
  • 2
  • 10
  • 33
  • 4
    No; do math on integers, only create a hex string when you need one - which is usually at the final output stage anyway. – Alex K. Mar 01 '16 at 13:50
  • You cannot represent a hexadecimal value as anything *other* than a string, considering that it has alpha characters in. Use the integer representation for manipulation, and convert it to hex for output. – Cody Gray - on strike Mar 01 '16 at 13:50
  • 1
    Ok gentlemen. Got it. Thank you – John P. Mar 01 '16 at 13:53
  • computers dont really think in integers or hex, they do 0's and 1's for them anything else is just the interpretation of the current value. – BugFinder Mar 01 '16 at 13:53
  • but a hexadecimal string is much *farther* away – Alex K. Mar 01 '16 at 13:55

5 Answers5

3

I mean a quick way such as int.Tostring("X") but without the int being turned to string.

No.

Look at this way. What is the difference between those?

var i = 10;
var i = 0xA;

As a value, they are exactly same. As a representation, first one is decimal notation and the second one is hexadecimal notation. The X you use hexadecimal format specifier which generates hexadecimal notation of that numeric value.

Be aware, you can parse this hexadecimal notation string to integer anytime you want.

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • I am experimenting with ReadProcessMemory. It's first parameter(address) is an IntPtr. Wouldn't it be a problem if the address is in hex format or dec? – John P. Mar 01 '16 at 14:08
  • @JohnP. `IntPtr` is native integer which it's size can be change as far as I know. I don't have enough information to answer your question, honestly. – Soner Gönül Mar 01 '16 at 14:13
2

There is no need to convert. Number ten is ten, write it in binary or hex, yes their representation will differ depending in which base you write them but value is same. So just add another integer to your integer - and convert the final result to hex string when you need it.


Take example. Assume you have

int x = 10 + 10; // answer is 20 or 0x14 in Hex.

Now, if you added

int x = 0x0A + 0x0A; // x == 0x14

Result would still be 0x14. See?

Numeric 10 and 0x0A have same value just they are written in different base.

Hexadecimal string although is a different beast. In above case that could be "0x14". For computer this would be stored as: '0', 'x', '1', '4' - four separate characters (or bytes representing these characters in some encoding). While in case with integers, it is stored as single integer (encoded in binary form).

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
1

I guess you missing the point what is HEX and what is INT. They both represent an numbers. 1, 2, 3, 4, etc.. numbers. There's a way to look at numbers: as natural numbers: INT and hexadecimal - but at the end those are same numbers. For example if you have number: 5 + 5 = 10 (int) and A (as hex) but it the same number. Just view on them are different

Alex F
  • 3,180
  • 2
  • 28
  • 40
1

Hex is just a way to represent number. The same statment is true for decimal number system and binary although with exception of some custom made numbers (BigNums etd) everything will be stored as binary as long as its integer (by integer i mean not floating point number). What would you really like to do is probably performing calculations on integers and then printing them as a Hex which have been already described in this topic C# convert integer to hex and back again

Community
  • 1
  • 1
user2184057
  • 924
  • 10
  • 27
1

The short answer: no, and there is no need.

The integer One Hundred and seventy nine (179) is B3 in hex, 179 in base-10, 10110011 in base-2 and 20122 in base-3. The base of the number doesn't change the value of it. B3, 17, 10110011, and 20122 are all the same number, they are just represented different. So it doesn't matter what base they are in as long as you do you mathematical operations on numbers in the same base it doesn't matter what the base is.

So in your case with Hex numbers, they can contain characters such as 'A','B', 'C', and so on. So when you get a value in hex if it is a number that will contain a letter in its hex representation it will have to be a string as letters are not ints. To do what you want, it would be best to convert both numbers to regular ints and then do math and convert to Hex after. The reason for this is that if you want to be able to add (or whatever operation) with them looking like hex you are going to to need to change the behavior of the desired operator on string which is a hassle.

Jacobr365
  • 846
  • 11
  • 24