/* A test about converting binary to decimal,Octal,HEX and Vice versa.
* 1.we shoud parse whatever we got from input to an Integer(decimal)
* 2.parse the decimal to binary,Octal,Hex using
* Integer.toHexString(int)
* Integer.toOctalString(int)
* Integer.toBinaryString(int)
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class hex_test {
int rem;
int number;
BufferedReader option;
String str;
String octal;
String hexadecimal;
String binary;
String name; public static void main(String args[]) {
hex_test hex = new hex_test();
System.out.println("Enter your option to convert");
System.out.println("1.Decimal, 2.Octal, 3.Hexadecimal, 4.Binary");
hex.choose();
hex.outPrint();
}
public void choose() {
try {
option = new BufferedReader(new InputStreamReader(System.in));
String op = option.readLine();
number = Integer.parseInt(op);
switch (number) {
case 1:
System.out.println("Enter a decimal:");
fromDecimal();
break;
case 2:
System.out.println("Enter an Octal:");
fromOctal();
break;
case 3:
System.out.println("Enter a Hexadecimal:");
fromHexa();
break;
case 4:
System.out.println("Enter a Binary:");
fromBinary();
break;
default:
System.out.println("The option is not accepted");
}
} catch (IOException e ) {
e.printStackTrace();
}
}
public String toDecimal() {
try {
str = option.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
} public void convert(int num) {
if (num != 2) {
octal = Integer.toOctalString(rem);
}
if (num != 3) {
hexadecimal = Integer.toHexString(rem);
}
if (num != 4) {
binary = Integer.toBinaryString(rem);
}
}
public void fromDecimal() {
rem = Integer.parseInt(toDecimal());
convert(number);
name = "Deciaml ";
} public void fromOctal() {
rem = Integer.parseInt(toDecimal(),8);
convert(number);
name = "Octal ";
}
public void fromBinary() {
rem = Integer.parseInt(toDecimal(),2);
convert(number);
name = "Binary ";
} public void fromHexa() {
rem = Integer.parseInt(toDecimal(),16);
convert(number);
name = "Hexadecimal ";
} public void outPrint() {
System.out.println("The input String is: " + name +"\t" + str);
if (number != 1) {
System.out.println(name + "Convert to decimal is:\t" + rem);
}
if (octal != null) {
System.out.println(name +"Convert to Octal is:\t" + octal);
}
if (hexadecimal != null) {
System.out.println(name +"Convert to Hexadecimal is:" + hexadecimal);
}
if (binary != null) {
System.out.println(name +"Convert to Binary is:\t" + binary);
}
}
}
输入选项后,我没做输入的检查,写完后发现代码有点长了,不知道有没有高手能精简一下
* 1.we shoud parse whatever we got from input to an Integer(decimal)
* 2.parse the decimal to binary,Octal,Hex using
* Integer.toHexString(int)
* Integer.toOctalString(int)
* Integer.toBinaryString(int)
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class hex_test {
int rem;
int number;
BufferedReader option;
String str;
String octal;
String hexadecimal;
String binary;
String name; public static void main(String args[]) {
hex_test hex = new hex_test();
System.out.println("Enter your option to convert");
System.out.println("1.Decimal, 2.Octal, 3.Hexadecimal, 4.Binary");
hex.choose();
hex.outPrint();
}
public void choose() {
try {
option = new BufferedReader(new InputStreamReader(System.in));
String op = option.readLine();
number = Integer.parseInt(op);
switch (number) {
case 1:
System.out.println("Enter a decimal:");
fromDecimal();
break;
case 2:
System.out.println("Enter an Octal:");
fromOctal();
break;
case 3:
System.out.println("Enter a Hexadecimal:");
fromHexa();
break;
case 4:
System.out.println("Enter a Binary:");
fromBinary();
break;
default:
System.out.println("The option is not accepted");
}
} catch (IOException e ) {
e.printStackTrace();
}
}
public String toDecimal() {
try {
str = option.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
} public void convert(int num) {
if (num != 2) {
octal = Integer.toOctalString(rem);
}
if (num != 3) {
hexadecimal = Integer.toHexString(rem);
}
if (num != 4) {
binary = Integer.toBinaryString(rem);
}
}
public void fromDecimal() {
rem = Integer.parseInt(toDecimal());
convert(number);
name = "Deciaml ";
} public void fromOctal() {
rem = Integer.parseInt(toDecimal(),8);
convert(number);
name = "Octal ";
}
public void fromBinary() {
rem = Integer.parseInt(toDecimal(),2);
convert(number);
name = "Binary ";
} public void fromHexa() {
rem = Integer.parseInt(toDecimal(),16);
convert(number);
name = "Hexadecimal ";
} public void outPrint() {
System.out.println("The input String is: " + name +"\t" + str);
if (number != 1) {
System.out.println(name + "Convert to decimal is:\t" + rem);
}
if (octal != null) {
System.out.println(name +"Convert to Octal is:\t" + octal);
}
if (hexadecimal != null) {
System.out.println(name +"Convert to Hexadecimal is:" + hexadecimal);
}
if (binary != null) {
System.out.println(name +"Convert to Binary is:\t" + binary);
}
}
}
输入选项后,我没做输入的检查,写完后发现代码有点长了,不知道有没有高手能精简一下
* 1.we shoud parse whatever we got from input to an Integer(decimal)
* 2.parse the decimal to binary,Octal,Hex using
* Integer.toHexString(int)
* Integer.toOctalString(int)
* Integer.toBinaryString(int)
*/
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class hex_test {
int rem;
int number;
BufferedReader option;
String str;
String octal;
String hexadecimal;
String binary; public static void main(String args[]) {
hex_test hex = new hex_test();
System.out.println("Enter your option to convert");
System.out.println("1.Decimal, 2.Octal, 3.Hexadecimal, 4.Binary");
hex.choose();
}
public void choose() {
try {
option = new BufferedReader(new InputStreamReader(System.in));
String op = option.readLine();
if (regex("^[1-4]$",op)) {
number = Integer.parseInt(op);
switch (number) {
case 1:
System.out.println("Enter a decimal:");
fromDecimal();
break;
case 2:
System.out.println("Enter an Octal:");
fromOctal();
break;
case 3:
System.out.println("Enter a Hexadecimal:");
fromHexa();
break;
case 4:
System.out.println("Enter a Binary:");
fromBinary();
break;
default:
System.out.println("The option is not accepted");
}
} else {
System.out.println("Please enter the right option");
}
} catch (IOException e ) {
System.out.println("You didn't enter any option");
}
}
public String toDecimal() {
try {
str = option.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
} public void convert(int num) {
if (num != 2) {
octal = Integer.toOctalString(rem);
}
if (num != 3) {
hexadecimal = Integer.toHexString(rem);
}
if (num != 4) {
binary = Integer.toBinaryString(rem);
}
} public boolean regex(String s,String m) {
Pattern pat = Pattern.compile(s);
Matcher mat = pat.matcher(m);
if ( mat.matches()) {
return true;
} else {
return false;
}
}
public void fromDecimal() {
if (regex("^\\d+$",toDecimal())) {
rem = Integer.parseInt(str);
convert(number);
outPrint("Decimal ");
} else {
System.out.println("Please enter a decimal");
}
} public void fromOctal() {
if ( regex("^[0-7]+$",toDecimal())) {
rem = Integer.parseInt(str,8);
convert(number);
outPrint("Octal ");
} else {
System.out.println("Please enter an Octal");
}
}
public void fromBinary() {
if (regex("^[0-1]+$",toDecimal())) {
rem = Integer.parseInt(str,2);
convert(number);
outPrint("Binary ");
} else {
System.out.println("Please enter a Binary");
}
} public void fromHexa() {
if ( regex("^[0-9[a-f]]+$",toDecimal())) {
rem = Integer.parseInt(str,16);
convert(number);
outPrint("Hexadecimal ");
} else {
System.out.println("Please enter a Hexadecimal");
}
} public void outPrint(String name) {
System.out.println("The input String is: " + name +"\t" + str);
if (number != 1) {
System.out.println(name + "Convert to decimal is:\t" + rem);
}
if (octal != null) {
System.out.println(name +"Convert to Octal is:\t" + octal);
}
if (hexadecimal != null) {
System.out.println(name +"Convert to Hexadecimal is:" + hexadecimal);
}
if (binary != null) {
System.out.println(name +"Convert to Binary is:\t" + binary);
}
}
}