bcd转换的工具类和方法
package wjz.utils;
import org.springframework.util.FileCopyUtils;
import java.io.*;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class WjzUtils {
public static void main(String[] args) throws ParseException {
}
public byte[] getBytes(String filePath){
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
public static String to16Hex(Date date){
Long ab = date.getTime()/1000;
String a = Long.toHexString(ab);
return a;
}
public static final int getLow4(byte data){
int low;
low = (data & 0x0f);
System.out.println(low);
return low;
}
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(" 0x");
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
public String decodeHexString(String str) {
str = HighLowHex(spaceHex(str));
String value = new BigInteger(str, 16).toString();
return value;
}
private String spaceHex(String str) {
char[] array = str.toCharArray();
if (str.length() <= 2) return str;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length; i++) {
int start = i + 1;
if (start % 2 == 0) {
buffer.append(array[i]).append(" ");
} else {
buffer.append(array[i]);
}
}
return buffer.toString();
}
private String HighLowHex(String str) {
if (str.trim().length() <= 2) return str;
List<String> list = Arrays.asList(str.split(" "));
Collections.reverse(list);
StringBuffer stringBuffer = new StringBuffer();
for (String string : list) {
stringBuffer.append(string);
}
return stringBuffer.toString();
}
public static String bcd2Str(byte[] bytes) {
StringBuffer temp = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
temp.append((byte) (bytes[i] & 0x0f));
temp.append((byte) ((bytes[i] & 0xf0) >>> 4));
}
return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp
.toString().substring(1) : temp.toString();
}
public static byte[] str2Bcd(String asc) {
int len = asc.length();
int mod = len % 2;
if (mod != 0) {
asc = "0" + asc;
len = asc.length();
}
byte abt[] = new byte[len];
if (len >= 2) {
len = len / 2;
}
byte bbt[] = new byte[len];
abt = asc.getBytes();
int j, k;
for (int p = 0; p < asc.length() / 2; p++) {
if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
j = abt[2 * p] - '0';
} else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
j = abt[2 * p] - 'a' + 0x0a;
} else {
j = abt[2 * p] - 'A' + 0x0a;
}
if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
k = abt[2 * p + 1] - '0';
} else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
k = abt[2 * p + 1] - 'a' + 0x0a;
} else {
k = abt[2 * p + 1] - 'A' + 0x0a;
}
int a = (k << 4) + j;
byte b = (byte) a;
bbt[p] = b;
}
return bbt;
}
public static byte[] longToBytes_Big(long l) {
byte b[] = new byte[8];
b[0] = (byte) (0xff & (l >> 56));
b[1] = (byte) (0xff & (l >> 48));
b[2] = (byte) (0xff & (l >> 40));
b[3] = (byte) (0xff & (l >> 32));
b[4] = (byte) (0xff & (l >> 24));
b[5] = (byte) (0xff & (l >> 16));
b[6] = (byte) (0xff & (l >> 8));
b[7] = (byte) (0xff & l);
return b;
}
}
|