Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #3195 > unrolled thread
| Started by | "Katak" <katak@THRWHITE.remove-dii-this> |
|---|---|
| First post | 2011-04-27 15:43 +0000 |
| Last post | 2011-04-27 15:43 +0000 |
| Articles | 9 — 4 participants |
Back to article view | Back to comp.lang.java.gui
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Need help to convert "Katak" <katak@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
Re: Need help to convert "Jeff Higgins" <jeff.higgins@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
Re: Need help to convert "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
Re: Need help to convert "Katak" <katak@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
Re: Need help to convert "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
Re: Need help to convert "Katak" <katak@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
Re: Need help to convert "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
Re: Need help to convert "tar" <tar@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
Re: Need help to convert "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
| From | "Katak" <katak@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:43 +0000 |
| Subject | Re: Need help to convert |
| Message-ID | <6c7359f1-70b1-4857-92c1-539f5aa40f0e@71g2000hse.googlegroups.com> |
To: comp.lang.java.gui
I wrote these codes below in Java and theres some problems passing the
string to do the conversion. Can anyone help me out? The c++ codes are
on the first post above. Thanks.
import java.util.Scanner;
public class BinaryToDecimal {
public static void main(String[] args) {
String bin;
int base = 10;
int result = 0;
int i;
double power = 0;
System.out.println("Enter a binary number: ");
Scanner input = new Scanner(System.in);
bin = input.nextLine();
for (i = bin.length()-1; i >=0; i--)
{
double cubed = java.lang.Math.pow ( 2, power);
result += (( bin[i] - 48)*(cubed));
power++;
}
System.out.print("Decimal = "+ result);
}
}
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24
[toc] | [next] | [standalone]
| From | "Jeff Higgins" <jeff.higgins@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:43 +0000 |
| Message-ID | <640wj.46$Md2.16@newsfe06.lga> |
| In reply to | #3195 |
To: comp.lang.java.gui
Katak wrote:
Lew suggested the String.charAt() method but,
if this is an exercize in converting
number representations by hand
see the comment;
else
why?YourConverter:MyConverter
import java.util.Scanner;
public class YourConverter {
public static void main(String[] args) {
String bin;
int result = 0;
double power = 0;
System.out.println("Enter a binary number: ");
Scanner input = new Scanner(System.in);
bin = input.nextLine();
//See Roedy Green's mindprod site for details on
//converting binary to decimal numbers
for (int i = bin.length(); i == 0; i--) {
double cubed = Math.pow(2, power);
result += ((bin.charAt(i) - 48) * (cubed));
power++;
}
System.out.print("Decimal = " + result);
}
}
Enter a binary number:
1101
Decimal = 0
public class MyConverter {
public static void main(String[] args) {
String hexString = "4341464542414245";
Long dec = Long.valueOf(hexString, 16);
String bin = Long.toBinaryString(dec);
System.out.println(dec + " : " + hexString);
System.out.println(bin);
}
}
4846231937305625157 :
4341464542414245
1000011010000010100011001
00010101000010010000010100001001000101
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24
[toc] | [prev] | [next] | [standalone]
| From | "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:43 +0000 |
| Message-ID | <e661s3delr44ve2uvg1esoc3vemina55rr@4ax.com> |
| In reply to | #3198 |
To: comp.lang.java.gui On Sat, 23 Feb 2008 16:17:52 -0500, "Jeff Higgins" <oohiggins@yahoo.com> wrote, quoted or indirectly quoted someone who said : > double cubed = Math.pow(2, power); for cubed I would use double cubed = x * x * x; Math.pow works by taking logs and exponentials, each of which is computed with a whacking huge polynomial approximation. With the arithmetic method, you don't lose precision on doubles that happen to be perfect ints. -- Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com --- * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet! --- Synchronet 3.15a-Win32 NewsLink 1.92 Time Warp of the Future BBS - telnet://time.synchro.net:24
[toc] | [prev] | [next] | [standalone]
| From | "Katak" <katak@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:43 +0000 |
| Message-ID | <90fadcfb-4497-48c9-a2d8-ca3af6c42894@s13g2000prd.googlegroups.com> |
| In reply to | #3200 |
To: comp.lang.java.gui On Feb 24, 6:08 am, Roedy Green <see_webs...@mindprod.com.invalid> wrote: > On Sat, 23 Feb 2008 16:17:52 -0500, "Jeff Higgins" > <oohigg...@yahoo.com> wrote, quoted or indirectly quoted someone who > said : > > > double cubed = Math.pow(2, power); > > for cubed I would use double cubed = x * x * x; > > Math.pow works by taking logs and exponentials, each of which is > computed with a whacking huge polynomial approximation. With the > arithmetic method, you don't lose precision on doubles that happen to > be perfect ints. > > -- > > Roedy Green Canadian Mind Products > The Java Glossaryhttp://mindprod.com Sorry. I think it was meant to be exponent since I was doing decimal = binary * 2^1 , decimal = binary * 2*2, etc. Named it wrongly. Thanks a lot. --- * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet! --- Synchronet 3.15a-Win32 NewsLink 1.92 Time Warp of the Future BBS - telnet://time.synchro.net:24
[toc] | [prev] | [next] | [standalone]
| From | "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:43 +0000 |
| Message-ID | <vet2s3d48eb7542n3vtkipbnc43m6v7q8d@4ax.com> |
| In reply to | #3202 |
To: comp.lang.java.gui On Sat, 23 Feb 2008 20:42:18 -0800 (PST), Katak <vampireex@hotmail.com> wrote, quoted or indirectly quoted someone who said : >Sorry. I think it was meant to be exponent since I was doing decimal = >binary * 2^1 , decimal = binary * 2*2, etc. Named it wrongly. In that case you can often use a loop to incrementally create a power, especially if you need to intermediate values. pow *= base; You can create powers of two with left shifts. e.g. 1 << 3 == 8 -- Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com --- * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet! --- Synchronet 3.15a-Win32 NewsLink 1.92 Time Warp of the Future BBS - telnet://time.synchro.net:24
[toc] | [prev] | [next] | [standalone]
| From | "Katak" <katak@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:43 +0000 |
| Message-ID | <cfbd828c-f3af-4995-bfa8-f38698468402@m23g2000hsc.googlegroups.com> |
| In reply to | #3198 |
To: comp.lang.java.gui
On Feb 24, 5:17 am, "Jeff Higgins" <oohigg...@yahoo.com> wrote:
> Katak wrote:
>
> Lew suggested the String.charAt() method but,
> if this is an exercize in converting
> number representations by hand
> see the comment;
> else
> why?YourConverter:MyConverter
>
> import java.util.Scanner;
>
> public class YourConverter {
> public static void main(String[] args) {
>
> String bin;
> int result = 0;
> double power = 0;
>
> System.out.println("Enter a binary number: ");
> Scanner input = new Scanner(System.in);
> bin = input.nextLine();
>
> //See Roedy Green's mindprod site for details on
> //converting binary to decimal numbers
>
> for (int i = bin.length(); i == 0; i--) {
> double cubed = Math.pow(2, power);
> result += ((bin.charAt(i) - 48) * (cubed));
> power++;
> }
> System.out.print("Decimal = " + result);
> }
>
> }
>
> Enter a binary number:
> 1101
> Decimal = 0
>
> public class MyConverter {
>
> public static void main(String[] args) {
>
> String hexString = "4341464542414245";
> Long dec = Long.valueOf(hexString, 16);
> String bin = Long.toBinaryString(dec);
>
> System.out.println(dec + " : " + hexString);
> System.out.println(bin);
>
> }
>
> }
>
> 4846231937305625157 :
> 4341464542414245
> 1000011010000010100011001
> 00010101000010010000010100001001000101
Thanks. I've fixed a lil bit of it and it works well now. Updated
codings are as below.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class BinaryToDecimal {
private static BufferedReader stdin= new BufferedReader(new
InputStreamReader(System.in));
public static void main(String[]args) throws IOException{
int result = 0;
double power = 0;
System.out.println("Enter a binary number: ");
String bin = stdin.readLine();
System.out.println("Binary = " + bin);
for (int i = bin.length()-1; i >= 0; i--) {
double exponent = Math.pow(2, power);
result += ((bin.charAt(i) - 48) * (exponent));
power++;
}
System.out.print("Decimal = " + result);
}
}
//Now for the gui part >_<
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24
[toc] | [prev] | [next] | [standalone]
| From | "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:43 +0000 |
| Message-ID | <mlt2s39rl8j9c813n610felfcm0vs2qn13@4ax.com> |
| In reply to | #3204 |
To: comp.lang.java.gui On Sat, 23 Feb 2008 20:46:10 -0800 (PST), Katak <vampireex@hotmail.com> wrote, quoted or indirectly quoted someone who said : > double exponent = Math.pow(2, power); > result += ((bin.charAt(i) - 48) * (exponent)); > power++; Shift would be more accurate and much much faster. power = 1; ... power <<= 1; or even better just shift (bin.charAt(i) - '0') directly by i. see http://mindprod.com/jgloss/shift.html http://mindprod.com/jgloss/binary.html http://mindprod.com/jgloss/precedence.html http://mindprod.com/jgloss/bit.html http://mindprod.com/jgloss/masking.html -- Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com --- * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet! --- Synchronet 3.15a-Win32 NewsLink 1.92 Time Warp of the Future BBS - telnet://time.synchro.net:24
[toc] | [prev] | [next] | [standalone]
| From | "tar" <tar@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:43 +0000 |
| Message-ID | <ymipruj33s0.fsf@blackcat.isi.edu> |
| In reply to | #3206 |
To: comp.lang.java.gui Roedy Green <see_website@mindprod.com.invalid> writes: > On Sat, 23 Feb 2008 20:46:10 -0800 (PST), Katak > <vampireex@hotmail.com> wrote, quoted or indirectly quoted someone who > said : > > > double exponent = Math.pow(2, power); > > result += ((bin.charAt(i) - 48) * (exponent)); > > or even better just shift (bin.charAt(i) - '0') directly by i. or even better: shift Character.digit(bin.charAt(i),2) -- Thomas A. Russ, USC/Information Sciences Institute --- * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet! --- Synchronet 3.15a-Win32 NewsLink 1.92 Time Warp of the Future BBS - telnet://time.synchro.net:24
[toc] | [prev] | [next] | [standalone]
| From | "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:43 +0000 |
| Message-ID | <k361s3pj466pjf9du1s2h5b21mpjba69vo@4ax.com> |
| In reply to | #3195 |
To: comp.lang.java.gui On Sat, 23 Feb 2008 11:06:36 -0800 (PST), Katak <vampireex@hotmail.com> wrote, quoted or indirectly quoted someone who said : >I wrote these codes below in Java and theres some problems passing the >string to do the conversion. for all your conversion needs, see http://mindprod.com/applet/converter.html It will show you for example, how to convert your double to a String you could feed to a JTextField. -- Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com --- * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet! --- Synchronet 3.15a-Win32 NewsLink 1.92 Time Warp of the Future BBS - telnet://time.synchro.net:24
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.gui
csiph-web