Kingamere
Kingamere

Reputation: 10166

C - convert from string to unsigned int

How do I convert from string to unsigned int?

I know that strtoul() converts from string to unsigned long int, but I want normal int and not long int.

I haven't been able to find a function that does that.

Upvotes: 2

Views: 3931

Answers (2)

alk
alk

Reputation: 70981

but I want normal [unsigned] int and not long [unsigned] int.

You want to use strtoul(), assign it to a long unsigned int, test the result for being in the range of [0..UINT_MAX] and if this is the case assign the long unsigned int to an unsigned int.

Upvotes: 2

Kingamere
Kingamere

Reputation: 10166

Just tested and found out that strtoul converts to a value equal to UINT_MAX so I guess this works.

Upvotes: 0

Related Questions