Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 849
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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