So I am programming a binary to decimal conversion program... But the twist is, I want to Convert my input string to a const char, and from the const char I want to convert it to an integer... Because I can not just convert a string directly to a integer.

My issue is, I want to get the length of the users input... Basically, if I input 111 the length would be 3, but later I want to input 111111 the length would be 6. I don't want the length to be predefined. From there I want to then place the seperate digits into an integer array, so that I can put it through my algorythm. Without knowing the length length of the input it becomes usless... lol.

My problem occurs when I am trying to convert a specific character in the string to an integer for storage in the array... I don't know if its possible, and if not I would gladly appreciate an alternative method of conversion without length limits.

Recommended Answers

All 3 Replies

I'm not sure I understand your problem (working with std::string is no less powerful than C-style strings), but if you're ultimately converting to an integer type length should be a consideration due to overflow.

>> I want to Convert my input string to a const char
The string class has a .c_str() member function, it returns a const char pointer.

>>Because I can not just convert a string directly to a integer

Why can't you? What method are you using for the conversion?

>>Basically, if I input 111 the length would be 3, but later I want to input 111111 the length would be 6. I don't want the length to be predefined. From there I want to then place the seperate digits into an integer array, so that I can put it through my algorythm. Without knowing the length length of the input it becomes usless

You do know that you can access the string just like you can access the char array, through the brackets operator[]?

>>My problem occurs when I am trying to convert a specific character in the string to an integer for storage in the array... I don't know if its possible, and if not I would gladly appreciate an alternative method of conversion without length limits

I think this is what you want :

string strNum = "123";
//use std::vector if you know them, i'm using raw arrays for simplicity
int array[100] = {};
for(int i = 0; i != strNum.size(); ++i){
  array[i] = strNum[i] - '0'; //convert the char digit to a decimal
 }

and now you got an array of ints

Thanks, I understand! I was a little confused :P I havent programmed in over a year... Forgot some stuff! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.