Monday, May 24, 2010

A c function to convert any number to base 10?

i need a few examples of a c(c programming language) functions that converts any number to base 10.

A c function to convert any number to base 10?
#include %26lt;stdlib.h%26gt;





char *p = "0x10";


long a = 0;





a = strtol(p, NULL, 16);





Edit: change the above to whatever you require. Base 2 would require a '2' instead of '16'. Base 109 would require a '109' instead of a '16'. The trick is to detect the base...





0x is a conventional start for a hex number.


0 is a conventional start for octal.


'b' is a conventional ending for binary...





Work out what base the number is (either using one of the mentioned conventions, or inventing your own) then use strtol as mentioned.


No comments:

Post a Comment