Link to home
Start Free TrialLog in
Avatar of maomorta
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
Avatar of Triskelion
Triskelion
Flag of United States of America image

Why don't you use the format specifiers to sprintf(), then return the modified value in a function?
ASKER CERTIFIED SOLUTION
Avatar of Triskelion
Triskelion
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of msmits
msmits

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;
}