public
class
ByteToString
{
public static void main(String args[]){
byte[] c={(byte)0x5A,(byte)0x08,(byte)0x00,(byte)0xFF,(byte)0xB2,(byte)0x33,(byte)0xDE,(byte)0xFF};
System.out.println (ByteToString.byte2HexString(c));
}
public static String byte2HexString(byte []c){
StringBuffer sb=new StringBuffer();
for (int i = 0; i<c.length; i++) {
String s=(String.format("%x",(int)c[i]&0xFF)).toUpperCase();
if(s.length()<2){
sb.append("0");
}
sb.append(s);
}
return sb.toString();
}
}
public static void main(String args[]){
byte[] c={(byte)0x5A,(byte)0x08,(byte)0x00,(byte)0xFF,(byte)0xB2,(byte)0x33,(byte)0xDE,(byte)0xFF};
System.out.println (ByteToString.byte2HexString(c));
}
public static String byte2HexString(byte []c){
StringBuffer sb=new StringBuffer();
for (int i = 0; i<c.length; i++) {
String s=(String.format("%x",(int)c[i]&0xFF)).toUpperCase();
if(s.length()<2){
sb.append("0");
}
sb.append(s);
}
return sb.toString();
}
}
public
class
ByteTools
{
/**
* byte转化为字符串输出
*
* @param b
* @return
*/
public static String byteToString(byte b) {
byte high, low;
byte maskHigh = (byte) 0xf0;
byte maskLow = 0x0f;
high = (byte) ((b & maskHigh) >> 4);
low = (byte) (b & maskLow);
StringBuffer buf = new StringBuffer();
buf.append(findHex(high));
buf.append(findHex(low));
return buf.toString();
}
public static String byteToString(byte[] bs) {
byte high, low;
byte maskHigh = (byte) 0xf0;
byte maskLow = 0x0f;
StringBuffer buf = new StringBuffer();
for (byte b : bs) {
high = (byte) ((b & maskHigh) >> 4);
low = (byte) (b & maskLow);
buf.append(findHex(high));
buf.append(findHex(low));
}
return buf.toString();
}
public static int stringToByte(String in, byte[] b) throws Exception {
if (b.length < in.length() / 2) {
throw new Exception("byte array too small");
}
int j = 0;
StringBuffer buf = new StringBuffer(2);
for (int i = 0; i < in.length(); i++, j++) {
buf.insert(0, in.charAt(i));
buf.insert(1, in.charAt(i + 1));
int t = Integer.parseInt(buf.toString(), 16);
System.out.println("byte hex value:" + t);
b[j] = (byte) t;
i++;
buf.delete(0, 2);
}
return j;
}
private static char findHex(byte b) {
int t = new Byte(b).intValue();
t = t < 0 ? t + 16 : t;
if ((0 <= t) && (t <= 9)) {
return (char) (t + '0');
}
return (char) (t - 10 + 'A');
}
}
/**
* byte转化为字符串输出
*
* @param b
* @return
*/
public static String byteToString(byte b) {
byte high, low;
byte maskHigh = (byte) 0xf0;
byte maskLow = 0x0f;
high = (byte) ((b & maskHigh) >> 4);
low = (byte) (b & maskLow);
StringBuffer buf = new StringBuffer();
buf.append(findHex(high));
buf.append(findHex(low));
return buf.toString();
}
public static String byteToString(byte[] bs) {
byte high, low;
byte maskHigh = (byte) 0xf0;
byte maskLow = 0x0f;
StringBuffer buf = new StringBuffer();
for (byte b : bs) {
high = (byte) ((b & maskHigh) >> 4);
low = (byte) (b & maskLow);
buf.append(findHex(high));
buf.append(findHex(low));
}
return buf.toString();
}
public static int stringToByte(String in, byte[] b) throws Exception {
if (b.length < in.length() / 2) {
throw new Exception("byte array too small");
}
int j = 0;
StringBuffer buf = new StringBuffer(2);
for (int i = 0; i < in.length(); i++, j++) {
buf.insert(0, in.charAt(i));
buf.insert(1, in.charAt(i + 1));
int t = Integer.parseInt(buf.toString(), 16);
System.out.println("byte hex value:" + t);
b[j] = (byte) t;
i++;
buf.delete(0, 2);
}
return j;
}
private static char findHex(byte b) {
int t = new Byte(b).intValue();
t = t < 0 ? t + 16 : t;
if ((0 <= t) && (t <= 9)) {
return (char) (t + '0');
}
return (char) (t - 10 + 'A');
}
}