binary to decimal conversion without using array (C programming)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
binary to decimal conversion without using array (C programming)
hi,
i have to convert binary number into decimal without using arrays,pointers(itoa and atoi() also no)..can someone plss give me a hint..i have no idea how can i do this without arrays..
hi,
i have to convert binary number into decimal without using arrays,pointers(itoa and atoi() also no)..can someone plss give me a hint..i have no idea how can i do this without arrays..
First, this definitely looks like homework
Second, have you thought about bitwise operators and bitshifting?
A number is a number. When you say convert from binary to decimal, (and you really want to use the itoa() call), i assume you want to create a character string of a decimal number, is that right?
Well, you COULD try bitwise operators, but here's something much simpler. It assumes a binary number of type long, and a receiving char array of at least 10 bytes, plus null terminator:
Code:
char *my_itoa(long binary, char *string)
{
long term = 1000000000;
char digit;
*string=0;
while (binary)
{
digit = '0';
while (binary >= term)
{
digit++;
binary -= term;
}
strncat(string,&digit,1);
term /= 10;
}
return string;
}
This assumes base (radix) of 10, and it always puts leading zeroes. With a little modification, you can eliminate the leading zeroes, etc.
(OK, so i'm helping with homework, where's the harm?)
The best advice on helping with HW that I've heard is to never say anything you may regret if the person you are helping joins your software team.
but the other side of the coin is that if you do everyones homework for them, they will not learn anything, and if you 'help' enough people, you should eventually get a increase in your salary due to lack of good programmers.
guys i'm sure u know english better than me, i sad give me a hint i didn't ask u to do my homework, do u really have nthg else to do, or this is how u increase the number of ur post..this is an open board so people can ask question..and my question was not about software teams,salary etc etc
A number is a number. When you say convert from binary to decimal, (and you really want to use the itoa() call), i assume you want to create a character string of a decimal number, is that right?
Well, you COULD try bitwise operators, but here's something much simpler. It assumes a binary number of type long, and a receiving char array of at least 10 bytes, plus null terminator:
Code:
char *my_itoa(long binary, char *string)
{
long term = 1000000000;
char digit;
*string=0;
while (binary)
{
digit = '0';
while (binary >= term)
{
digit++;
binary -= term;
}
strncat(string,&digit,1);
term /= 10;
}
return string;
}
This assumes base (radix) of 10, and it always puts leading zeroes. With a little modification, you can eliminate the leading zeroes, etc.
(OK, so i'm helping with homework, where's the harm?)
Hi dogpatch,
thx for ur replay..well i'm not suposed to use pointers or function str***, itoa(), atoi().. i wrotte the code about decimaly to binary conversion yesterday:
Code:
#include <stdio.h>
main() {
int a,i,bit,b,j;
printf("\ndec number: ");
scanf( "%d", &a);
for (j = sizeof(a) * 8;j >= 0; j--) {
b = a >> j;
printf("%d", (b & 1));
}
printf("\n");
system("PAUSE");
}
This was easy, but how to convert single bits , without array..if i use array i can do it very easy....
i start 0*2^0 + 1*2^1 ....+x*2^n
Hi dogpatch,
thx for ur replay..well i'm not suposed to use pointers or function str***, itoa(), atoi().. i wrotte the code about decimaly to binary conversion yesterday:
Code:
#include <stdio.h>
main() {
int a,i,bit,b,j;
printf("\ndec number: ");
scanf( "%d", &a);
for (j = sizeof(a) * 8;j >= 0; j--) {
b = a >> j;
printf("%d", (b & 1));
}
printf("\n");
system("PAUSE");
}
This was easy, but how to convert single bits , without array..if i use array i can do it very easy....
i start 0*2^0 + 1*2^1 ....+x*2^n
thx once more
linuxi
This is what I don't get. The program above uses arrays (in the form of strings) and pointers throughout. Instead of converting a decimal number (i.e., an int) to a binary representation in an array (i.e., a string) it converts it into a binary representation in an array implicitly (i.e., a buffer). The problem seems impossible as posed.
Can you give us the exact requirements of your homework?
guys i'm sure u know english better than me, i sad give me a hint i didn't ask u to do my homework, do u really have nthg else to do, or this is how u increase the number of ur post..this is an open board so people can ask question..and my question was not about software teams,salary etc etc
no i do not have anything else to do.. and yes this is how i get my post count up. i have only ever answered 2 questions since i registered
if you really want a hint reread the posts. someone already mentioned the bitwise operators. here is another hint, you are going to need to use the left shift (<<) and the or (|) operators (and of course = ). thats it. the conversion part consists of using one of those, an if statement, and then another one of those. put that inside of a loop and you are done. of yeh you will need to declare an int to store the conversion in..
and i think that you are taking the string restriction too literally. you need a string to store the input string that is in binary form.
oh and you do not need to use * ^ or any thing other than the ones i mentioned above. if you are getting much more than 10 lines of code in the conversion part of this program then you are off track (i did it in 3 not counting the loop)..
So now, linuxi, you want to accept user input of a binary string using scanf(), and then convert that string of 0's and 1's to a decimal number which you will display back with a printf() statement - is that about right?
As osor and xhi both pointed out, this will necessarily involve a string to hold the binary '0' and '1' characters.
I also agree with xhi's hints. (I did it in two lines, not counting the loop).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.