maomorta
asked on
Base conversions (Decimal Octal Hexadecimal)
Hi
I need four simple functions:
1 - Convert Hexadecimal to Decimal
2- Convert Octal to Decimal
3- Convert Decimal to Hexadecimal
I can use the format specifiers in printf but I prefer to have a function that returns a valuer into a variable
4- Convert Decimal to Octal
I can use the format specifiers in printf but I prefer to have a function that returns a valuer into a variable
Thanks
I need four simple functions:
1 - Convert Hexadecimal to Decimal
2- Convert Octal to Decimal
3- Convert Decimal to Hexadecimal
I can use the format specifiers in printf but I prefer to have a function that returns a valuer into a variable
4- Convert Decimal to Octal
I can use the format specifiers in printf but I prefer to have a function that returns a valuer into a variable
Thanks
Why don't you use the format specifiers to sprintf(), then return the modified value in a function?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No that difficult with strtol(), which has the base as third parameter.
As an example Decimal to Hex (no error checking).
3.
char *dec2hex(char *in)
{
char *out = malloc(100);
char *tmp;
sprintf(out, "%lx", strtol(in, &tmp, 10));
return out;
}
As an example Decimal to Hex (no error checking).
3.
char *dec2hex(char *in)
{
char *out = malloc(100);
char *tmp;
sprintf(out, "%lx", strtol(in, &tmp, 10));
return out;
}