今天写代码碰到了一个问题就是byte[]数组转换为十六进制,在网上搜了一下,顺便整理了一下,我是这么转的:
<span style="white-space:pre"> </span>StringBuffer sb2 = new StringBuffer("");
for (int i = 0; i < msgInfo.length; i++) {
sb2.append("0x");
byte high, low;
byte maskHigh = (byte)0xf0;
byte maskLow = 0x0f;
high = (byte)((msgInfo[i] & maskHigh) >> 4);
low = (byte)(msgInfo[i] & maskLow);
sb2.append(findHex(high));
sb2.append(findHex(low));
}
调用的方法为:
/**
* byte---->char
* @param b
* @return
*/
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');
}
为了方便以后使用,便整理了一些,以备不时之需:
package chenqk.util;
/**
* 通过位移操作实现byte[],int,long,string之间的转换
* @author chenqk
*
*/
public class ByteIntStringLong {
public ByteIntStringLong() {
}
/**
* @param args
*/
public static void main(String[] args) {
}
/*----------------------------------byte转换-------------------------------------------*/
/**
* byte---->char
* @param b
* @return
*/
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---->string
* @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();
}
/**
* byte---->int
* 会出现两种情况
* 1.一种是要求保持值不变,例如进行数值计算,可采用强制类型转换
* 2.另一种是要求保持最低字节中各个位不变,3个高字节全部用0填充,例如进行编解码操作
* @param b
* @return
*/
public static int byteToInt(byte b){
int num = b & 0xff;// 第一种情况
int num2 = (int) b;// 第二种情况
return num;
}
/**
* byte---->short
* @param b
* @return
*/
public static short byteToShort(byte b){
short num = (short)b;
return num;
}
/**
* byte---->long
* @param b
* @return
*/
public static long byteToLong(byte b){
long num = (long)b;
return num;
}
/*-------------------------------------byte[]转换--------------------------------------*/
/**
* byte[]---->char
* @param b
* @return
*/
public static char byteToChar(byte[] b) {
char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
return c;
}
/**
* byte[]---->short
*
*/
public final static short getShort(byte[] buf, boolean asc, int len) {
short r = 0;
if (asc)
for (int i = len - 1; i >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0x00ff);
}
else
for (int i = 0; i < len; i++) {
r <<= 8;
r |= (buf[i] & 0x00ff);
}
return r;
}
/**
* byte[]---->int
* @param buf
* @param asc
* @param len
* @return
*/
public final static int getInt(byte[] buf, boolean asc, int len) {
if (buf == null) {
throw new IllegalArgumentException("byte array is null!");
}
if (len > 4) {
throw new IllegalArgumentException("byte array size > 4 !");
}
int r = 0;
if (asc)
for (int i = len - 1; i >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0x000000ff);
}
else
for (int i = 0; i < len; i++) {
r <<= 8;
r |= (buf[i] & 0x000000ff);
}
return r;
}
/**
* byte[]---->int
* @param buf
* @param asc
* @return
*/
public final static int getInt(byte[] buf, boolean asc) {
if (buf == null) {
throw new IllegalArgumentException("byte array is null!");
}
if (buf.length > 4) {
throw new IllegalArgumentException("byte array size > 4 !");
}
int r = 0;
if (asc)
for (int i = buf.length - 1; i >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0x000000ff);
}
else
for (int i = 0; i < buf.length; i++) {
r <<= 8;
r |= (buf[i] & 0x000000ff);
}
return r;
}
/**
* byte[]---->long
* @param buf
* @param asc
* @return
*/
public final static long getLong(byte[] buf, boolean asc) {
if (buf == null) {
throw new IllegalArgumentException("byte array is null!");
}
if (buf.length > 8) {
throw new IllegalArgumentException("byte array size > 8 !");
}
long r = 0;
if (asc)
for (int i = buf.length - 1; i >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0x00000000000000ff);
}
else
for (int i = 0; i < buf.length; i++) {
r <<= 8;
r |= (buf[i] & 0x00000000000000ff);
}
return r;
}
/*-----------------------------------------其他转化为byte[]-----------------------------------*/
/**
* string---->byte[]
* @param hexString
* @return
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
// d[i] = (byte) (charToByte(hexChars[pos]) << 4 |
// charToByte(hexChars[pos + 1]));
}
return d;
}
/**
* char---->byte[]
*
* @param c
* @return
*/
public static byte[] charToByte(char c) {
byte[] b = new byte[2];
b[0] = (byte) ((c & 0xFF00) >> 8);
b[1] = (byte) (c & 0xFF);
return b;
}
/* B2 -> 0xB2 */
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;
}
/**
* int---->byte[]
* @param num
* @return
*/
public static byte[] intToBytes(int num) {
byte[] b = new byte[4];
for (int i = 0; i < 4; i++) {
b[i] = (byte) (num >>> (24 - i * 8));
}
return b;
}
/**
* short---->byte[]
* @param num
* @return
*/
public static byte[] shortToBytes(short num) {
byte[] b = new byte[2];
for (int i = 0; i < 2; i++) {
b[i] = (byte) (num >>> (i * 8));
}
return b;
}
/**
* short---->byte[]
* @param s
* @param asc
* @return
*/
public final static byte[] getBytes(short s, boolean asc) {
byte[] buf = new byte[2];
if (asc)
for (int i = buf.length - 1; i >= 0; i--) {
buf[i] = (byte) (s & 0x00ff);
s >>= 8;
}
else
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (s & 0x00ff);
s >>= 8;
}
return buf;
}
/**
* int---->byte[]
* @param s
* @param asc
* @return
*/
public final static byte[] getBytes(int s, boolean asc) {
byte[] buf = new byte[4];
if (asc)
for (int i = buf.length - 1; i >= 0; i--) {
buf[i] = (byte) (s & 0x000000ff);
s >>= 8;
}
else
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (s & 0x000000ff);
s >>= 8;
}
return buf;
}
/**
* long---->byte[]
* @param s
* @param asc
* @return
*/
public final static byte[] getBytes(long s, boolean asc) {
byte[] buf = new byte[8];
if (asc)
for (int i = buf.length - 1; i >= 0; i--) {
buf[i] = (byte) (s & 0x00000000000000ff);
s >>= 8;
}
else
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (s & 0x00000000000000ff);
s >>= 8;
}
return buf;
}
}
除了关于这些基本类型(String除外,因为string为final类),还整理出了进制之间的转换:
int n1 = 14;
//十进制转成十六进制:
Integer.toHexString(n1);
//十进制转成八进制
Integer.toOctalString(n1);
//十进制转成二进制
Integer.toBinaryString(12);
//十六进制转成十进制
Integer.valueOf("FFFF",16).toString();
//十六进制转成二进制
Integer.toBinaryString(Integer.valueOf("FFFF",16));
//十六进制转成八进制
Integer.toOctalString(Integer.valueOf("FFFF",16));
//八进制转成十进制
Integer.valueOf("576",8).toString();
//八进制转成二进制
Integer.toBinaryString(Integer.valueOf("23",8));
//八进制转成十六进制
Integer.toHexString(Integer.valueOf("23",8));
//二进制转十进制
Integer.valueOf("0101",2).toString();
//二进制转八进制
Integer.toOctalString(Integer.parseInt("0101", 2));
//二进制转十六进制
Integer.toHexString(Integer.parseInt("0101", 2));