I want to know if there is any function in the C language which can convert base of decimal number binary or Decimal to Hexadecimal or etc.
Asked
Active
Viewed 9,434 times
0
-
You told us C but tagged the question asp.net? Do you mean C#? – Liath Jan 15 '14 at 09:37
-
Also what is a dacible?? and what do you mean by ***binary or D to X or etc.***? – RononDex Jan 15 '14 at 09:39
-
What do you mean base? Decimals are all base 10? Do you want to round it? – Liath Jan 15 '14 at 09:39
-
@RononDex I took a guess he meant decimal... – Liath Jan 15 '14 at 09:39
-
@Liath If that is so, this is a duplicate of http://stackoverflow.com/questions/923771/quickest-way-to-convert-a-base-10-number-to-any-base-in-net – RononDex Jan 15 '14 at 09:41
-
However your real question is, for sure simple google query will answer you. – Konrad Kokosa Jan 15 '14 at 09:42
-
If you really mean C despite the asp.net tag, perhaps you're looking for the itoa or ltoa functions. These are not part of ANSI-C but are supported by some compilers. Google can tell you more. – Joe Jan 15 '14 at 09:47
2 Answers
6
You can try this..
int convert(int number,int base){
if(number == 0 || base==10)
return number;
return (number % base) + 10*convert(number / base, base);
}
int main () {
int a;
scanf("%d",&a);
//Lets convert 123 in decimal to octal
printf("%d\n",convert(123, 8));
return 0;
}
Let me know if this helps.. :)

Rashad
- 11,057
- 4
- 45
- 73
-
i m not able to implement this code pls send completely code. thank you – Pushpendra Jan 15 '14 at 10:20
-
As i have Studies this code, this can convert only Decimal number to Octal, this can not convert Decimal Number into another Base. – Pushpendra Jan 15 '14 at 10:41
-
@user3197468: The main() function is just an example. Change the 8 into whatever base you want. – Lee Netherton Jan 15 '14 at 10:43
-
If you want to convert decimal to binary call the function like convert(123, 2). – Rashad Jan 15 '14 at 10:45
-
-
@user3197468 >> Try to understand the logic except searching for code.. I am sure that the time you spent searching for code, if you tried to understand the logic you could write it yourself. We are here to help not to supply only the code and make others lazy.. :) – Rashad Jan 16 '14 at 03:29
-
Thanks sir, But I got the answer of this question there is one particular function which can convert the base of an given number. the function is itoa(number,array,base) – Pushpendra Jan 16 '14 at 04:58
-
As far as I can remember this function has some compilation issue.. Hope you can overcome this. :) – Rashad Jan 16 '14 at 05:00
1
There is no standard libraries or functions to do that
You can always write a small function to do your calculations.. It won't be hard to find codes (If you don't wanna write) but I suggest to write them your self with your basic knowledge on number conversions..

Kavindu Dodanduwa
- 12,193
- 3
- 33
- 46
-
2Dear Friends I got the answer of this question there is one particular function which can convert the base of an given number. the function is itoa(number,array,base) – Pushpendra Jan 15 '14 at 12:04
-
1Yes, that's an easy way :) but there could be limitation on compiling supprt, that's why I suggested.. anyway solved the problem – Kavindu Dodanduwa Jan 15 '14 at 14:28