problem in converting a hex string to a dec number

I have a function called "distance" which returns a user defined type "OverlayKey" which is a HEX number. Since I need to convert it to a DEC
number, I've thought to convert it to an integer with cstdlib standard function "strtol" but the problem is that "strtol" wants in input a "const char*"
type, so I casted the output of "distance" as follows:

const char* d = (const char*) distance(thisNode.getKey(),successorList->getSuccessor().getKey());

and then I applied the const char* d to the funcion strtol

long int dist = strtol(d,NULL,16) << "\n";


but the problem is that the cast doesn't work:
error: invalid cast from type ‘OverlayKey’ to type ‘char*’

I have two questions:

- how to cast the user defined OverlayKey to const char*
- if not possible, how to convert a HEX string to a DEC number

Thanks!!!
http://www.cplusplus.com/reference/iostream/manipulators/dec/

after thinking about it, I guess that wouldn't convert it permanently, so I don't have an answer right now
Last edited on
how to convert a HEX string to a DEC number
1
2
3
4
5
6
7
8
9
#include <sstream>

int main()
{
	std::string hexStr = "0xAA";
	std::istringstream iss(hexStr);
	int decNum;
	iss >> std::hex >> decNum;	
}


how to cast the user defined OverlayKey to const char*
How is OverlayKey defined?
Last edited on
applying the solution of Peter87 i get this error:

error: conversion from ‘OverlayKey’ to non-scalar type ‘std::string {aka std::basic_string<char>}’ requested

here is the code:

std::string hexStr = distance(thisNode.getKey(),successorList->getSuccessor().getKey());
std::istringstream iss(hexStr);
long long decNum;
iss >> std::hex >> decNum;
OverlayKey can't be implicitly converted to a string. We don't know what OverlayKey is or how it has been defined so we don't know how to solve this.
Last edited on
OverlayKey is a user defined type; some kind of class, is it, or struct?

In this case, the compiler does not know how to deal with it. Show us OverlayKey and we can tell you how to get the data from inside it and convert it from hex to int.
Topic archived. No new replies allowed.