我已经计算了余数,但我的问题是从余数到新的十六进制字符的实际转换。在 Dec2Hex 类中,我需要做的就是从超类中获取余数组列表(我已经这样做了(,然后使用字符数组 hexDigits[] 将其转换为字符串。我必须在循环中逐步遍历其余数组列表并构建十六进制字符串,但我不确定如何做到这一点。
//this is the Binary to Decimal implementation which hex2dec is based off of
package business;
import java.util.ArrayList;
/**
*
* @author
*/
public class Bin2Dec implements Conversion{
public static final String VALUEDESC ="Binary";
public static final String RESULTDESC ="Decimal";
private String origval, result;
private ArrayList<String> resultsteps;
private String emsg;
private boolean valid;
public Bin2Dec(String value) {
emsg = "";
origval = value;
if (valid =isValid(value)) { //valid will be true or false
convert(value);
} else {
emsg = "Illegal, binary value (must be only zeros and ones";
}
}
private boolean isValid(String v) {
for (int i=0; i< v.length(); i++) {
if (v.charAt(i) != '1' && v.charAt(i) != '0') {
return false;
}
}
return true;
}
// we can do isValid because we declared private boolean valid
@Override
public boolean isValid() {
return this.valid;
}
private void convert(String v) {
long r = 0;
String reverse = new StringBuilder(v).reverse().toString();
resultsteps = new ArrayList<>();
for (int i=0; i < reverse.length(); i++) {
if (reverse.charAt(i) == '1') {
long p = (long) Math.pow(2,i);
r += p;
resultsteps.add("There is a(n) " + p + " in the number (2^" + i + ")" );
}
}
this.result = String.valueOf(r);
}
@Override
public String getResult() {
return this.result;
}
@Override
public ArrayList<String> getProcessLog() {
return this.resultsteps;
}
@Override
public String getErrorMsg() {
return this.emsg;
}
@Override
public String getValue() {
return this.origval;
}
}
实现
//this is the hex2dec implementation I created
package business;
import java.util.ArrayList;
/**
*
* @author
*/
public class Dec2Hex extends Dec2Num {
public static final String VALUEDESC="Decimal";
public static final String RESULTDESC="Hexadecimal";
// private ArrayList<String> codes;
public static final int BASE=16;
private String binaryresult;
char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'};
String hex = "";
public Dec2Hex(String value) {
super(value, Dec2Hex.BASE);
this.binaryresult="";
}
@Override
public String getResult() {
if(!super.isValid()) {return "Invalid Value";}
for(Integer i : super.getRemainders()) { //we deleted remainders in Dec2Bin so we call super
String hex = Integer.toHexString(i);
this.binaryresult += String.valueOf(i);
}
switch (hex) {
}
return this.binaryresult;
}
// public ArrayList<String> getHexConversion(int d) {
// char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
// 'A', 'B', 'C', 'D', 'E', 'F'};
// String hex = "";
// while (d > 0) {
// int digit = d % BASE;
// hex = hexDigits.charAt(digit) + hex;
// d = d / BASE;
// }
// return codes;
// }
// public ArrayList<String> getHexConversion(int d) {
//
// String hex = "";
// while (d > 0) {
// int digit = d % BASE;
// char hexDigits = (hex <= 9 && hex >= 0);
// (char)(hexDigits + '0') : (char)(hexDigits- 10 + 'A');
// }
// return this.binaryresult;
// }
}
接口和抽象实现
//interface
public interface Conversion {
public String getValue();
public String getResult();
public boolean isValid();
public ArrayList<String> getProcessLog();
public String getErrorMsg();
//each subclass should fulfill these methods
}
//abstract implementation
public abstract class Dec2Num implements Conversion{
private String origval, emsg;
private boolean valid;
private int base;
private ArrayList<String> resultsteps;
private ArrayList<Integer> remainders;
public Dec2Num(String value, int base) { //int base b/c it needs to know what to divide down by
emsg ="";
origval = value;
this.base = base;
try {
long n = Long.parseLong(value);
if (n < 0 ) {
emsg = "Bad decimal value: must be positive.";
this.valid = false;
} else {
this.valid = true;
resultsteps = new ArrayList<>();
remainders = new ArrayList<>();
convertByRecur(n);
}
} catch (NumberFormatException e) {
emsg = "Illegal value: not a decimal integer";
this.valid = false;
}
}//end of constructor
@Override
public boolean isValid() {
return this.valid;
}
private void convertByRecur(long n) {
int r = 0;
r = (int)(n % this.base);
long newval = n / this.base;
resultsteps.add(n + " divided by " + this.base + "="
+ newval + " w/remainder of: " +r);
if (newval > 0) {
convertByRecur(newval); //recursive call
}
remainders.add(r);
}
@Override
public ArrayList<String> getProcessLog() {
return this.resultsteps;
}
@Override
public String getErrorMsg() {
return this.emsg;
}
@Override
public String getValue() {
return this.origval;
}
@Override
public abstract String getResult();
protected ArrayList<Integer> getRemainders() {
return this.remainders;
}
}
首先,调用Integer.toHexString()
应该有效,但它绕过了hexDigits
数组的想法。唯一的潜在问题是它会给你小写字母,a
尽管f
你可能想要大写的等效字母。
String.valueOf(i)
不正确。如果i
是 7,它会给你7
,但如果i
11,它会给你11
,你想要B
,十六进制数字。你可以只做
this.binaryresult += hex;
你根本不需要你的switch
声明。
假设您确实想使用hexDigits
数组:
- 要从数组中获取正确的
char
,只需使用hexDigits[i]
。 您可以将
char
直接追加到String
,而无需先将其转换为String
:this.binaryresult += yourChar;
在提供我的代码版本之前,我会让你稍微挣扎一下。我相信你能做到。