473,466 Members | 1,363 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how do I make a binary to decimal converter?

i need to use integer division to extract the digits. This is more of a math issue than a programming issue i think.This is what i have so far:
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class TremorinKeithA2Q2 {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.     String biInput="";
  8.     int len;
  9.     int biPow;
  10.     int i;
  11.     int product;
  12.     int bi = 0;
  13.     int lastDigit;
  14.     int trunc;
  15.  
  16.         Scanner kbd = new Scanner(System.in);
  17.  
  18.         while(bi!=-1){
  19.             System.out.println("Enter a binary number");
  20.             biInput = kbd.next();
  21.             bi = Integer.parseInt(biInput);
  22.             len =biInput.length();
  23.  
  24.             for(i = len-1; i>=0; i--){
  25.                 biPow= (int)Math.pow(2,i);
  26.                 lastDigit = bi%10;
  27.                 product = lastDigit * biPow;
  28.                 trunc = bi/10;
  29.  
  30.            }
  31.  
  32.  
  33.  
  34.  
  35. }
  36. }
  37. }
  38.  
I don't know where to go from here.
Oct 20 '10 #1
3 1963
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
The logic is wrong. I suggest you modify this logic.

Regards
Dheeraj Joshi
Oct 22 '10 #2
Nepomuk
3,112 Recognized Expert Specialist
Let's have a look at what this algorithm has to do.

Say, we have the input 101010 and should turn this into a decimal number. Now, 101010 means 1*32 + 0*16 + 1*8 + 0*4 + 1*2 + 0*1, so we'll start at the last digit and work our way forward from there.

As the computer is using an integer, which works on the basis of 10, to get the last digit use
Expand|Select|Wrap|Line Numbers
  1. biInput % 10
In this case, this will give you the digit 0.

Then, you want to get to the next digit. There are several ways of doing that, but the simplest method is to remove the last digit (here: the 0) and then check which is the new last digit. It's not difficult. :-)

Greetings,
Nepomuk
Oct 22 '10 #3
santaji
7 New Member
  • Multiplication is happening in reverse direction
  • "product" variable is not updated to do sum of previous state and current state
  • "bi" variable is not updated even after getting last digit
Oct 25 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
9
by: Gaijinco | last post by:
Is there any way to declare a variable as a binary so that if the variable "var" holds the value of 1001, then ++var = 1010?
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
9
by: bowsayge | last post by:
Inspired by fb, Bowsayge decided to write a decimal integer to binary string converter. Perhaps some of the experienced C programmers here can critique it. It allocates probably way too much...
3
by: Brian Henry | last post by:
Does anyone know of or know how to convert a COBOL packed decimal in a text file to a decimal that .NET can work with? we are importing Old COBOL data files that have packed data in them and need...
8
by: vendredi5h | last post by:
Hello all, I'd like to split a 64-bits word (hold as a floating point number) into two 32-bits words (hold as integers). Bitwise operators are working on Integers, not on floating point. ...
1
by: girl23 | last post by:
Hi there ]i am new to C programming and totally lost. iam trying to convert decimal to binary. here is what i did please ignore the case h and m. I am trying to get case 'b' to work. i do not...
3
by: logaelo | last post by:
Hello all, Could anyone explain how to optimization this code? In the prosess of optimization what is the factor needed and important to know about it? Thank you very much for all. ...
2
by: yinka90 | last post by:
Hi. I have a problem with my converter. It works fine. the only problem is that it doesnt convert single digit numbers properly. public class Main { /** * @param args the command line...
0
by: Terry Reedy | last post by:
A. Joseph wrote: These are number representation systems that can be applied to or used with integral, rational (numberator,denominator), and 'point' numbers. Try Wikipedia or any search...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.