-3

I have an int where the value is supposed to represent a hex value but is stored without the 0x prefix. For example, int x is stored as 20 but is supposed to equal 32 because it is missing the 0x. How do I convert this so that the computer understands to interpret the int as hex rather than decimal?

EDIT: for example, I want to convert 50 to 0x50.

connoraw
  • 173
  • 1
  • 2
  • 11
  • So the value can only represent 5/8 of all the numbers? That's weird. – Jerry Jeremiah May 08 '17 at 03:58
  • possible duplicate [of this](http://stackoverflow.com/questions/3464194/how-can-i-convert-an-integer-to-a-hexadecimal-string-in-c) – Mathews Sunny May 08 '17 at 03:59
  • Possible duplicate of [How can I convert an integer to a hexadecimal string in C?](http://stackoverflow.com/questions/3464194/how-can-i-convert-an-integer-to-a-hexadecimal-string-in-c) – Mathews Sunny May 08 '17 at 04:00
  • 5
    Number bases are just a way to write a number. x is the same if it is written `0x20` or `32` or `thirty-two` or `treinta y dos`... they are all just ways of saying the same conceptual number. So this only makes a difference when reading or writing a string. `printf("%x", x)` will print it in hex whereas `printf("%d", x)` will print in decimal... – Adam D. Ruppe May 08 '17 at 04:00
  • No, I want to convert `50` to `0x50` for example. – connoraw May 08 '17 at 04:00
  • 1
    So the user types `50` or you have the string `"50"` and you want to do `int x = some_function(that_string);` such that `x == 0x50`? (spoilers: some_function is `strtol`) – Adam D. Ruppe May 08 '17 at 04:04
  • 1
    @williamsca97 When you say "int x is stored as 20 but is supposed to equal 32", You must be seeing this in debugger/IDE's watch window. Right click on that and choose mode as Decimal (works on Most IDEs). It will show you 32. Hex, Decimal, Octal, Binary are just way of representation. For CPU, it's all binary! – Swanand May 08 '17 at 04:05
  • 2
    Let's change your example and see if it makes sense. "For example, int x is stored as 1F but is supposed to equal 31 becuase it is missing the 0x. No, this doesn't make sense. Not at all. Please re-explain what you are trying to do. – Richard Schwartz May 08 '17 at 04:08
  • 3
    Are you saying you want to convert a given number, say 50 (0x32), to a different number, say 80 (0x50), such that the *hex* digits of the new number match the *decimal* digits of the old number? (whew, mouthful) – Erik Nyquist May 08 '17 at 04:08
  • Simply add `0x` on front of it and it will get hex notation. If that isn't the answer you are looking for, you need to clarify the question and show just exactly in what format those values are and why you can't simply edit them. – Lundin May 08 '17 at 06:47
  • Otherwise you can use this code, which doesn't make any sense either. `#include #define MY_CODE_IS_CRAP(x) 0x##x int main (void) { printf("%d", MY_CODE_IS_CRAP(20)); }` – Lundin May 08 '17 at 06:48

1 Answers1

1

you can use as below. 'hex' is your number without 0x. hexStr will contain actual value.

// number in hex format without 0x
int hex = 20;
char hexStr[10];

// int to string
sprintf(hexStr,"0x%d",hex);

If your want to use it as int:

int number = (int)strtol(hexStr, NULL, 16);
RK_Aus
  • 886
  • 1
  • 11
  • 18