Content of a char* into an unsigned long int

Hi,

I've searched on how to do this and i can't find a solution. Could someone help?

How can i get the content of a char* into an unsigned long int without converting the content?

I have this:

char * str = "0b00100010110110101010100010100010";

And i need this:

unsigned long int theValue = 0b00100010110110101010100010100010;

Why?
Because i'm constructing that string dynamically in code, gluing several pieces to get the final result, and than i need it as an unsigned long int to be passed to a function that requires it.

Thank you.

use strtoul()

KeithRB:
use strtoul()

However, don't include any "b". Just digits.

I was going to let him find that out. 8^)

Yes, i've tried it, maybe i'm using it wrong.

I tried:

char * str = "0b00100010110110101010100010100010";
char *pEnd;
unsigned long int theValue = strtoul(str, &pEnd, 0);

And i tried also with:

char * str = "0b00100010110110101010100010100010";
char *pEnd;
unsigned long int theValue = strtoul(str, NULL, 0);

Edit: About the "b"... if i don't use the "b" the function that gets parameter doesn't give the expected results.

Then start the conversion at &str[2]
This will start at the '0' on the other side of the "0b"

unsigned long int theValue = strtoul(&str[2], NULL, 2);

The "0b" is non-standard. It appears that gcc accepts "0b" for constants but has not changed strtoul() to handle it.

Thank you. It worked. :slight_smile: