-1

I want to convert the hex address to an integer. I browsed through stackoverflow and someone advised to used reinterpret_cast for the purpose. Here is my code.

class sample
{
    public:
        int x;
        sample *next;
        sample()
        {
            x=0;
        }
};

int main()
{
    sample *objptr = new sample();
    long int x = reinterpret_cast<long int&>(objptr);
    cout<<x<<" "<<objptr;
    return 0;
}

Is there any other way to achieve the same? What if I have the octal representation for the same? How do i convert hex and octal to integer and vice versa in C++?

ikegami
  • 343,984
  • 15
  • 249
  • 495
Kitpul Bhatt
  • 34
  • 1
  • 7
  • 28
  • Octal and hex representations would be a textual string format. Where are they? Otherwise there is no such thing as a "hex address." In C if you want to output a pointer you would use format `%p` and cast the pointer with `(void*)`. – Weather Vane Feb 16 '20 at 18:27
  • sorry if that was abuse of terms, edited it – Kitpul Bhatt Feb 16 '20 at 18:30
  • 1
    `long int x = reinterpret_cast(objptr);` is wrong. It will not do what you seem to want. Did you mean `auto x = reinterpret_cast(objptr);`? – walnut Feb 16 '20 at 18:30
  • how do i convert hex and octal to integer and vice versa in c and c++? – Kitpul Bhatt Feb 16 '20 at 18:30
  • @PulkitBhatnagar So you want the variable's address printed in decimal? – walnut Feb 16 '20 at 18:31
  • In C you can convert from a string in any base (up to 36) using `strtol`. But I can't see a reason why the string's value would be a pointer. – Weather Vane Feb 16 '20 at 18:32
  • @PulkitBhatnagar Having a reference there does not do what you want. Have you actually tested the code you are showing? – walnut Feb 16 '20 at 18:32
  • cmon guyz I showed you code you also show me some code. – Kitpul Bhatt Feb 16 '20 at 18:33
  • @walnut yep its working – Kitpul Bhatt Feb 16 '20 at 18:33
  • @PulkitBhatnagar Ah ok, I see why it prints seemingly the correct address. It is still wrong though, since it is an aliasing violation. I showed you the correct method in the first comment. – walnut Feb 16 '20 at 18:37
  • first comment, thats for displaying, what about storing – Kitpul Bhatt Feb 16 '20 at 18:38
  • @PulkitBhatnagar Different representations for integers are only relevant for printing. Different representations still represent *exactly* the same integer. It doesn't make sense to ask in what representation a number is stored. You can use `std::hex`, `std::oct` and `std::dec` to choose how `< – walnut Feb 16 '20 at 18:40
  • what if I want to calculate hash for the integer? will it consider hex or the int? – Kitpul Bhatt Feb 16 '20 at 18:41
  • @PulkitBhatnagar The question makes no sense. How do you want to calculate the hash? – walnut Feb 16 '20 at 18:42
  • just a hash function to calculate hash for an integer – Kitpul Bhatt Feb 16 '20 at 18:43
  • how about implementimg map from address to int, I would be needing the hex as integer – Kitpul Bhatt Feb 16 '20 at 18:43
  • 2
    @PulkitBhatnagar You have a fundamental misunderstanding what numbers and their representations are. "*hex*" is a possible *representation* of an integer. It is not distinct from integers. – walnut Feb 16 '20 at 18:45
  • ok let me tell it clearly. see I want to implement a hashmap from linked list nodes address(key) to the integer(value). So I made an array that will be my hash table kind of. Now since all operation needs to be done in O(1), I wrote a hash function that takes integer as input gets a hash value that is the position in my array where the integer will be written. Now since the address I was getting is in hex format so when I performed my hash function to it, I got an error ` error: invalid operands of types ‘sample*’ and ‘int’ to binary ‘operator* ` – Kitpul Bhatt Feb 16 '20 at 18:49
  • @PulkitBhatnagar If you have a concrete problem that you want to solve, then please ask about that problem and not about an issue you have with your attempted solution. What you have here is a typical [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). The question here is not whether the address is "*in hex format*", the question is whether you obtained the address as actual *integer type* or whether you obtained it as a *string representation*. If you did the latter, then you already went in the wrong direction. There should be no strings involved. – walnut Feb 16 '20 at 18:57
  • thanks for patiently listening to me, however if you could have answered my query that would be a lot better. BTW it was former – Kitpul Bhatt Feb 16 '20 at 18:59

2 Answers2

0

Something like this?

int main(int argc, char** argv)
{
  // this is your object
  int* i = new int(0);

  // as stated by walnut
  auto addr = reinterpret_cast<std::uintptr_t>(i);

  std::cout << std::dec << addr << std::endl;
  std::cout << std::oct << addr << std::endl;
  std::cout << std::hex << addr << std::endl;
}
Dimfred
  • 173
  • 1
  • 15
-1

Your question is based on a false premise; on a misconception.

Addresses aren't hex, or decimal, or octal, or binary, or anything else.

You do not have "the hex address", or "the octal representation for the same", ever.

Addresses are addresses.

Even if they were the same thing conceptually as numbers, they still wouldn't be hex, or decimal, or octal, or binary, or anything else, because those are bases used when representing numbers, i.e. printing them to screen. They are not a property of the number; they are not a part of its value, nor of its type.

Numbers are numbers.

With that out of the way, what you need to do is get a number from an address, then print that number in your desired representation.

The first one's easy: cast it to std::uintptr_t in C++11 (or just uintptr_t in C99). You don't need to use reinterpret_cast for that; you could use a C-style cast instead. But you do need to cast. Then output it however you like, just as you would output any other number, in whatever base you like.

Ironically, that's probably why you think these things are "hex" or "octal", because the only way you can ever see them on screen is when somebody has done these steps for you.

Asteroids With Wings
  • 16,602
  • 2
  • 20
  • 35