Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #3198
| From | "Jeff Higgins" <jeff.higgins@THRWHITE.remove-dii-this> |
|---|---|
| Subject | Re: Need help to convert |
| Message-ID | <640wj.46$Md2.16@newsfe06.lga> (permalink) |
| Newsgroups | comp.lang.java.gui |
| References | <6c7359f1-70b1-4857-92c1-539f5aa40f0e@71g2000hse.googlegroups.com> |
| Date | 2011-04-27 15:43 +0000 |
| Organization | TDS.net |
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
Back to comp.lang.java.gui | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web