|
Yeong
|
Well, this is the java code that lets u convert decimal to binary ~ 36-base number. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import java.util.*; public class Convert { public static String convertbase(int n, int base) { // special case if (n == 0) return "0";
String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String s = ""; while (n > 0) { int d = n % base; s = digits.charAt(d) + s; n = n / base; } return s; } public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter the number base 10 to convert: "); int original = keyboard.nextInt(); System.out.print("Enter the new base: "); int base = keyboard.nextInt(); String answer = convertbase(original,base); System.out.println("The number "+original+"[base 10] = "+answer+"[base "+base+"]"); } } |
|
|
|
|
|
Logged
|
Project Redemption.... My project progresses: HEREMy Pastebin stuffs: HERECheck your rate: HEREMy Animations: HEREノ◕ヮ◕)ノ:・゚ PENGUIN WAVE!!:„ø¤º°¨ ¨°º¤KEEP THE PENGUIN GOING ¸„ø¤º°¨ ¨°º¤øº LETS GO PENGUIN !¤¤º°¨¨°º¤øº¤ø„¸¸ø¤º°¨„ ø¤º°¨¨°º
|
|
|
|
Munchor
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import java.util.*; public class Convert { public static String convertbase(int n, int base) { // special case if (n == 0) return "0";
String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String s = ""; while (n > 0) { int d = n % base; s = digits.charAt(d) + s; n = n / base; } return s; } public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter the number base 10 to convert: "); int original = keyboard.nextInt(); System.out.print("Enter the new base: "); int base = keyboard.nextInt(); String answer = convertbase(original,base); System.out.println("The number "+original+"[base 10] = "+answer+"[base "+base+"]"); } } |
This is looking good. It seems like it supports all bases. Here's how it works for me: 1 2 3 4 5 6 7 8 9 10
| david@DavidPC:~/Documentos/Java$ javac Convert.java david@DavidPC:~/Documentos/Java$ java Convert Enter the number base 10 to convert: 65535 Enter the new base: 16 The number 65535[base 10] = FFFF[base 16] david@DavidPC:~/Documentos/Java$ java Convert Enter the number base 10 to convert: 255 Enter the new base: 2 The number 255[base 10] = 11111111[base 2]
|
|
|
|
|
« Last Edit: 26 March, 2011, 00:03:23 by Scout »
|
Logged
|
|
|
|
|
Yeong
|
Well, that's what supposed to look like.  BTW, don't try incredibly huge number or it will go overflow 
|
|
|
|
|
Logged
|
Project Redemption.... My project progresses: HEREMy Pastebin stuffs: HERECheck your rate: HEREMy Animations: HEREノ◕ヮ◕)ノ:・゚ PENGUIN WAVE!!:„ø¤º°¨ ¨°º¤KEEP THE PENGUIN GOING ¸„ø¤º°¨ ¨°º¤øº LETS GO PENGUIN !¤¤º°¨¨°º¤øº¤ø„¸¸ø¤º°¨„ ø¤º°¨¨°º
|
|
|
|
nemo
|
then to go from any base to another... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import java.util.Scanner; public class Test{ public static void main(String[] args){ Scanner intRead = new Scanner(System.in); Scanner strRead = new Scanner(System.in); System.out.print("Enter a number to convert: "); String src = strRead.nextLine(); System.out.print("Enter the number's base: "); int oldBase = intRead.nextInt(); System.out.print("Enter the desired base: "); int newBase = intRead.nextInt(); String ans = Integer.toString(Integer.valueOf(src, oldBase), newBase); System.out.println(src + " in base " + oldBase + " is " + ans + " in base " + newBase); } }
|
|
|
|
|
|
Logged
|
|
|
|
|