binary conversion question 2

Okay I am writing a program that will convert a 8 digit binary number into decimal, then output the decimal number, then the binary number broken down 1 integer per line

here is my code

import javax.swing.JOptionPane;

publicclass binaryconverter{

publicstaticvoid main(String[] args){

String binum_st;

int binum , newnum, pickednum1 ,pickednum2 ,pickednum3 ,pickednum4 ,pickednum5 ,pickednum6 =0,pickednum7 ,pickednum8 , converted,dec;

binum_st= JOptionPane.showInputDialog(null,"Enter first number");

binum = Integer.parseInt(binum_st);

newnum = binum;

pickednum8 = newnum % 10;

dec = pickednum8 * 1;

newnum = newnum / 10;

pickednum7 = newnum % 10;

dec = (pickednum7 * 2) + dec;

newnum = newnum /10;

pickednum6 = newnum % 10;

dec = (pickednum6 * 4) + dec;

newnum = newnum /10;

pickednum5 = newnum % 10;

dec = (pickednum5 * 8) + dec;

newnum = newnum /10;

pickednum4 = newnum % 10;

dec = (pickednum4 * 16) + dec;

newnum = newnum /10;

pickednum3 = newnum % 10;

dec = (pickednum3 * 32) + dec;

newnum = newnum /10;

pickednum2 = newnum % 10;

dec = (pickednum2 * 64) + dec;

newnum = newnum /10;

pickednum1 = newnum % 10;

dec = (pickednum1 * 128 ) + dec;

System.out.println(dec);

System.out.println(pickednum8+" " + pickednum7 +" " + pickednum6+" " + pickednum5+" " + pickednum4+" " +pickednum3+" " +pickednum2+" " +pickednum1);

}

}

here is the output

31

11111000

what I want is

31

1

1

1

1

1

(note that the zero's are gone)

can anyone help me accomplish this?

Thanks

[2327 byte] By [joshuapbella] at [2007-11-27 8:28:23]
# 1

Sorry before my stuff was messed up.

This program should give you enough a hint to do what you want.

import java.io.*;

public class ono

{

public static void main(String[] args) throws IOException

{

String strBinaryNumber = "11111000";

char[] arrBinary = strBinaryNumber.toCharArray();

int intResult = 0;

int intMult = 1;

for(int x = 0; x<arrBinary.length;x++)

{

intMult = realpow(x,2);

if(arrBinary[x] == '1')

{

System.out.print("1");

intResult = intResult + intMult;

System.out.println(""+intMult);

}

else

{

System.out.println("0");

}

}

}

public static int realpow( int intPow, int intNum)

{

int intOrig = intNum;

if(intPow == 0)

return 1;

for(int x = 1; x >< intPow; x++)

intNum = intOrig*intNum;

return intNum;

}

}

Message was edited by:

ZimmerS1337

ZimmerS1337a at 2007-7-12 20:18:21 > top of Java-index,Java Essentials,Java Programming...
# 2
...
prometheuzza at 2007-7-12 20:18:21 > top of Java-index,Java Essentials,Java Programming...
# 3
terrifying ain't it?
ejpa at 2007-7-12 20:18:21 > top of Java-index,Java Essentials,Java Programming...
# 4
> terrifying ain't it?It made me cry a little bit.
prometheuzza at 2007-7-12 20:18:21 > top of Java-index,Java Essentials,Java Programming...
# 5
are you guys saying this because i was too lazy to look up how to do this properly with bitwise operators?
ZimmerS1337a at 2007-7-12 20:18:21 > top of Java-index,Java Essentials,Java Programming...
# 6

ZimmerS1337, please listen. This sort of 'help' is worse than useless. It isn't even a good example of coding anything, let alone the OP's problem. The code is poor, indeed it is largely nonsense; it doesn't do what the OP wants; and it doesn't do anything else sensible either, at least nothing that I can figure out in the limited time I have available for this sort of thing. You've even got the binary representation of his initial number (31) wrong.

If you want to post code please first get rid of the unused variable initializations and computations, and state what it's supposed to do, or at least what you think it's supposed to do. Perhaps you could also reconsider the 10 lines of code to compute x*x? which is wrong anyway as you should be multiplying by two somewhere in this loop, or shifting, not squaring.

For the record, the OP's problem can be expressed in 6 lines of code:

String strBinaryNumber = "00011111";

intnumber = Integer.parseInt(strBinaryNumber, 2);

System.out.println(number);

strBinaryNumber = Integer.toBinaryString(number); // remove leading 0's

for (int i = 0; i < strBinaryNumber.length(); i++)

System.out.println(strBinaryNumber.charAt(i));

However I presume he isn't supposed to use library methods for this assignment, so he is back with his original problem of how to convert binary to decimal, which is what he's supposed to be learning here, and I'm not doing anyone's homework for them. The IT field is crowded enough without cheats.

But if you want to offer advice or a draft solution please make it both sensible and relevant.

ejpa at 2007-7-12 20:18:21 > top of Java-index,Java Essentials,Java Programming...
# 7

> are you guys saying this because i was too lazy to

> look up how to do this properly with bitwise

> operators?

No. It was of how the OP's code looks like AND your lengthy hint, the fact you did not use code conventions, your realpow() method is redundant (there's one in the java.lang.Math class) and it is very confusing! If you want to get 2^5, you have to call that method like this: realpow(5, 2), which most people will recognize as being 5^2 == 25.

prometheuzza at 2007-7-12 20:18:21 > top of Java-index,Java Essentials,Java Programming...
# 8

The code really does convert a binary number i have tested it. But you're right i shouldnt just write something out in quick 10 minutes or it may confuse someone further. I dont know about multiplying by 2... the fourth position is equal to 2^3 ... also i tried using the one in math but sometimes it wouldnt always return the right value because for some reason the one in math requires doubles.

Message was edited by:

ZimmerS1337

ZimmerS1337a at 2007-7-12 20:18:21 > top of Java-index,Java Essentials,Java Programming...
# 9

Strange testing. Your code really doesn't 'convert a binary number', in fact it doesn't convert anything to anything. For a binary string of "1111000" it should print 248 somewhere, unless you are numbering your bits backwards from everybody else in the universe, in which case it should print 31. It doesn't do either. What it prints is this:

11

14

18

116

132

0

0

0

which is very interesting but (a) wrong and (b) not the answer being looked for.

And now that I understand that 'realpow(x,y' is supposed to compute y^x not x^y, it is still wrong, and replacing these 10 lines of non-working code by 1 << x, which also expresses the intent correctly, does at least yield a result which makes some sense. FYI a correct version of 'realpow()' takes 4 lines, not 7:

intresult = 1;

for(int x = 1; x <= intPow; x++)

result *= intNum;

return result;

ejpa at 2007-7-12 20:18:21 > top of Java-index,Java Essentials,Java Programming...
# 10

public class BinToDecimal {

public static void main(String[] args) {

String num = "00011111";

int decFalseValue = Integer.parseInt(num);

/**creating the binary number**/

int[] arr = new int[8];

int i = 0;

int remainder = 0;

while(decFalseValue > 1){

remainder = decFalseValue%10;

decFalseValue = decFalseValue/10;

if(i >= 8)

break;

arr = remainder;

i++;

}

arr = decFalseValue;

/**getting the real decimal value*/

int decRealValue = 0;

for(i = 0;i<8;i++){

if(arr == 1)

decRealValue += BinToDecimal.realpow(i,2) * arr;

}

System.out.println(decRealValue);

for(i = 0;i<8;i++){

System.out.println(arr);

}

}

public static int realpow( int intPow, int intNum)

{

int intOrig = intNum;

if(intPow == 0)

return 1;

for(int x = 1; x < intPow; x++)

intNum = intOrig*intNum;

return intNum;

}

}

cowboy4mhella at 2007-7-12 20:18:21 > top of Java-index,Java Essentials,Java Programming...
# 11

> String num = "00011111";

> int decFalseValue = Integer.parseInt(num);

> ...

NOOO!

It is nonsense and unlogical to parse a String of a number in binary representation as a decimal number and then back again!

String s = "00011111";

int decimal = Integer.parseInt(s, 2);

String binary = Integer.toBinaryString(decimal);

System.out.println(s+" == "+decimal+" == "+binary);

prometheuzza at 2007-7-12 20:18:21 > top of Java-index,Java Essentials,Java Programming...
# 12
So the point of programming isn't to create as much confusion as possible?
EvilBroa at 2007-7-12 20:18:21 > top of Java-index,Java Essentials,Java Programming...
# 13
> So the point of programming isn't to create as much> confusion as possible?; )
prometheuzza at 2007-7-12 20:18:21 > top of Java-index,Java Essentials,Java Programming...
# 14
> So the point of programming isn't to create as much> confusion as possible?that depends on why and where you're programming.If you're a consultant trying to make himself indispensible...
jwentinga at 2007-7-12 20:18:21 > top of Java-index,Java Essentials,Java Programming...
# 15
cowboy4mhell forgot the smileys ... I hope ... if not could he please read reply #6 (and all the others)
ejpa at 2007-7-21 22:41:02 > top of Java-index,Java Essentials,Java Programming...
# 16

Hmmm, i'm terribly sorry about this post. Yesterday i was going on 4 hours of sleep and not thing clearly. I will make sure such an abomination doesnt occur again. By the way how do you guys avoid reinventing the wheel. I find myself often implementing things that I find out are in the Java API.

ZimmerS1337a at 2007-7-21 22:41:02 > top of Java-index,Java Essentials,Java Programming...
# 17
look in the API first rather than afterwards?
ejpa at 2007-7-21 22:41:02 > top of Java-index,Java Essentials,Java Programming...