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
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 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.