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

Recommended Answers

All 4 Replies

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

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!

REMARKS:

  • 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;

Why not using a stringstream ?

Because its overkill. A simple subtraction will do as previously posted.

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.