• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 851
  • Last Modified:

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
0
maomorta
Asked:
maomorta
  • 2
1 Solution
 
TriskelionCommented:
Why don't you use the format specifiers to sprintf(), then return the modified value in a function?
0
 
msmitsCommented:
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;
}
0

Featured Post

Become a Leader in Data Analytics

Gain the power to turn raw data into better business decisions and outcomes in your industry. Transform your career future by earning your MS in Data Analytics. WGU’s MSDA program curriculum features IT certifications from Oracle and SAS.  

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now