: Hi!
: is there a way to convert value of a string into an integer? i am doing a simultaneous equation program that needs this.
:
: Thank you.
: Abhishek
:
http://deydas.net
:
If you're using 0 to fill in missing terms, then sscanf will return the number of conversions instead of a value.
#include <stdio.h>
#include <string.h>
int main(void)
{
int n;
printf("hex: %2d\n", sscanf("FF", "%x", &n) == 1 ? n : -1);
printf("oct: %2d\n", sscanf("377", "%o", &n) == 1 ? n : -1);
printf("dec: %2d\n", sscanf("255", "%d", &n) == 1 ? n : -1);
return (0);
}
It *should* work with dec, hex, or oct values.