十六进制字符串转换为整型:int sum = Integer.parseInt(this.getLoadMoney(), 16) / 100;
十进制字符串转换为长整型:long balance = (Long.parseLong(this.getBalance(), 10))/100;
/**
* 产生随机数
* @return
*/
public String rand() {
return Integer.toHexString(new Random().nextInt());
}
/**
* 格式化date
* @param value
* @return
*/
public String date(Date value) {
return new SimpleDateFormat("yyMMdd").format(value);
}
/**
* 格式化time
* @param value
* @return
*/
public String time(Date value) {
return new SimpleDateFormat("HHmmss").format(value);
}
//pPara字符串格式化为pDigit字节长度字符串
public static String trimPara(String pPara, int pDigit) {
if (pPara.length() > pDigit) {
pPara = pPara.substring(pPara.length() - pDigit);
} else if (pPara.length() < pDigit) {
int i = 0;
int pLength = pPara.length();
while ((i + pLength) < pDigit) {
pPara = "0" + pPara;
i++;
}
}
return pPara;
}
/**
* 十六进制转字符串
* @param hexString 十六进制字符串
* @param encodeType 编码类型4:Unicode,2:普通编码
* @return 字符串
*/
public static String hexStringToString(String hexString, int encodeType) {
String result = "";
int max = hexString.length() / encodeType;
for (int i = 0; i < max; i++) {
char c = (char) DigitalConvert.hexStringToAlgorism(hexString.
substring(i * encodeType, (i + 1) * encodeType));
result += c;
}
return result;
}
/**
* 十六进制字符串转byte
* @param hex 十六进制字符串
* @return byte数组
*/
public static byte[] hexStringToByte(String hex) {
int max = hex.length() / 2;
byte[] bytes = new byte[max];
String binarys = DigitalConvert.hexStringToBinary(hex);
for (int i = 0; i < max; i++) {
bytes[i] = (byte) DigitalConvert.binaryToAlgorism(binarys.substring(
i * 8 + 1,
(i + 1) * 8));
if (binarys.charAt(8 * i) == '1') {
bytes[i] = (byte) (0 - bytes[i]);
}
}
return bytes;
}
/**
* 十六进制字符串装十进制
* @param hex 十六进制字符串
* @return 十进制数值
*/
public static int hexStringToAlgorism(String hex) {
hex = hex.toUpperCase();
int max = hex.length();
int result = 0;
for (int i = max; i > 0; i--) {
char c = hex.charAt(i - 1);
int algorism = 0;
if (c >= '0' && c <= '9') {
algorism = c - '0';
}
else {
algorism = c - 55;
}
result += Math.pow(16, max - i) * algorism;
}
return result;
}
/**
* 二进制转十进制
* @param binary 二进制字符串
* @return 十进制数值
*/
public static int binaryToAlgorism(String binary) {
int max = binary.length();
int result = 0;
for (int i = max; i > 0; i--) {
char c = binary.charAt(i - 1);
int algorism = c - '0';
result += Math.pow(2, max - i) * algorism;
}
return result;
}
/**
* 十六转二进制
* @param hex 十六进制字符串
* @return 二进制字符串
*/
public static String hexStringToBinary(String hex) {
hex = hex.toUpperCase();
String result = "";
int max = hex.length();
for (int i = 0; i < max; i++) {
char c = hex.charAt(i);
switch (c) {
case '0':
result += "0000";
break;
case '1':
result += "0001";
break;
case '2':
result += "0010";
break;
case '3':
result += "0011";
break;
case '4':
result += "0100";
break;
case '5':
result += "0101";
break;
case '6':
result += "0110";
break;
case '7':
result += "0111";
break;
case '8':
result += "1000";
break;
case '9':
result += "1001";
break;
case 'A':
result += "1010";
break;
case 'B':
result += "1011";
break;
case 'C':
result += "1100";
break;
case 'D':
result += "1101";
break;
case 'E':
result += "1110";
break;
case 'F':
result += "1111";
break;
}
}
return result;
}