string to char

stephencushen
stephencushen used Ask the Experts™
on
hexstr= 6D

for (counter = 0; counter <= 3; counter++)
{
   string binNow = hexstr.substr (counter,1);

   //i need to convert the string binNow into a char
   //this char will the be passed into a function Hex2Dec
   //in the function is a simple switch statement
   //which returns an int based on the char value passed
   //in

   int ret = Hex2Dec(binNow);          
}



i have tried everything i can think off.

anyone have any idea's?


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
To get the first char of an std::string, use:

std::string binNow;
// ...
char ch = binNow[0];

Commented:
There is a method in the string class that returns a C style string(char* array) of the string. So:

const char* cstring = hexstr.c_str();

now you can do the following:

int ret = *cstring[i]; //to get the individual char's

hope this helps!

Commented:
Actually, try this:

for (counter = 0; counter <= 3; counter++)
{
  char binNow = hexstr[counter];
  // ...
}

Commented:
If you're trying to convert a Hex string to an integer, you can use the following function:

template<typename T> int HexStrToInt(const T HexStr)
{
     std::istringstream ist(HexStr);
     int value;
     ist >> std::hex >> value;
     return value;
}

Author

Commented:
man i a retard,

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial