0

I was having a bit of trouble trying to interpret my input as hexadecimal numbers. For example if given the input of 40A2, and assuming I have an array at size 216, how would I access the following example?

arr[0x40A2] //this is simply accessing the array at binary 0100 0000 1010 0010 
              which is at index 16546

I'm just having a bit of trouble with this because as the program receives input from the user, numbers come in interpreted as ASCII values, and need to be interpreted as hexadecimal values.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
user1871869
  • 3,317
  • 13
  • 56
  • 106
  • 1
    possible duplicate of [C++ convert hex string to signed integer](http://stackoverflow.com/questions/1070497/c-convert-hex-string-to-signed-integer) – Cornstalks Mar 06 '13 at 16:59
  • http://stackoverflow.com/questions/5040681/how-to-read-hex-values-from-a-file-using-fstream-in-c – Derek Mar 06 '13 at 17:00

1 Answers1

2

How do you read the input? C++ standard input streams understand the std::hex modifier:

unsigned int index;
std::cin >> std::hex >> index;
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214