i have to get an integer out of the unsigned char string and assign it to unsigned int varable....
i mean if i have
unsigned char array = { "123"};
i simply want to get either 1 or 2 or 3 out of it
and assign it to
unsigned int variable..

9 Years
Discussion Span
Last Post by Ancient Dragon

unsigned int val = (unsigned int)array[0]; // For the value 1

If the OP wants the value 49, this will work. I think the OP might want the value 1 so subtract 48 from val afterwards. But then again, he might mean something completely different.

Please clarify exactly what you want.


Why not using a stringstream ?

unsigned char arr[] = "4564";
unsigned int val = 0;
stringstream ss;
ss << arr; // write the array to the stringstream
val = ss.get() - '0'; // get next digit and store it in 'val'

This is only an example, you'll have to adapt it to your needs, if you want to get the next digit, then you just invoke [B]val = ss.get() - '0';[/B] .

Hope this helps!


  • If you're using stringstreams in your code, then you'll have to include the sstream header: #include <sstream> .
  • Also this example doesn't provide checking to see whether the end of the number has already been reached.
  • Getting the whole number is even easier: ss >> val;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.