LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2006, 03:16 PM   #1
linuxi
LQ Newbie
 
Registered: Oct 2005
Posts: 13

Rep: Reputation: 0
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..
 
Old 11-04-2006, 03:53 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Is this homework? All I can say is / and % are your friends.
 
Old 11-04-2006, 04:09 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by linuxi
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?

Last edited by osor; 11-04-2006 at 04:15 PM.
 
Old 11-04-2006, 09:35 PM   #4
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
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?)

Last edited by dogpatch; 11-04-2006 at 09:40 PM.
 
Old 11-04-2006, 10:00 PM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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.
 
Old 11-04-2006, 10:11 PM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by tuxdev
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.
 
Old 11-05-2006, 04:18 AM   #7
linuxi
LQ Newbie
 
Registered: Oct 2005
Posts: 13

Original Poster
Rep: Reputation: 0
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
 
Old 11-05-2006, 04:29 AM   #8
linuxi
LQ Newbie
 
Registered: Oct 2005
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dogpatch
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

thx once more
linuxi
 
Old 11-05-2006, 11:56 AM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by linuxi
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?
 
Old 11-05-2006, 12:30 PM   #10
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by linuxi
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)..
 
Old 11-05-2006, 08:17 PM   #11
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
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).
 
Old 11-05-2006, 09:00 PM   #12
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by dogpatch
(I did it in two lines, not counting the loop).
yeh i guess mine was two also.. i had the if statement taking up two lines (i hate to be outdone )
 
Old 11-10-2006, 05:21 PM   #13
linuxi
LQ Newbie
 
Registered: Oct 2005
Posts: 13

Original Poster
Rep: Reputation: 0
Today was my exam, and thx god i had to do "decimal to binary conversion" and not "binary to decimal"..but still thx for ur help

linuxi
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
representation of binary tree using array sajith Programming 3 10-06-2005 10:59 PM
cout conversion to binary... audibel Programming 3 03-12-2005 07:06 AM
Decimal to binary maldini1010 Programming 6 02-01-2005 04:03 PM
c++ binary to decimal conversion.. miguetoo Programming 13 04-24-2003 09:49 PM
decimal comma notation conversion to dot? heaslein69 Linux - Software 2 01-25-2002 04:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration