c - Converting a number to string with multiple arguments -
i want convert number x of base n string , store in str. str has max size of max. in program don't want use library functions. if reach maximum size of array, function should return false , array contents should undefined.
the prototype of function looks this:
bool num2str(int x, char *str, unsigned n, unsigned max);
how go making work? i'm having trouble understanding algorithm behind it.
i need check value of n, did that:
bool num2str(int x, char *str, unsigned n, unsigned max) { assert(n >= 2 && n <= 36); return true; }
but that's do. please help.
let's take number in base 10: 123456. let's repeatedly apply module , integer division using number , base 10:
123456 mod 10 = 6 123456 div 10 = 12345 12345 mod 10 = 5 12345 div 10 = 1234
as can see, module base extracts last digit, while integer division base shifts number on digit right. can same base. hope hint enough.
Comments
Post a Comment