- //byte 与 int 的相互转换
- public static byte intToByte(int x) {
- return (byte) x;
- }
- public static int byteToInt(byte b) {
- //Java 总是把 byte 当做有符处理;我们可以通过将其和 0xFF 进行二进制与得到它的无符值
- return b & 0xFF;
- }
- //byte 数组与 int 的相互转换
- public static int byteArrayToInt(byte[] b) {
- return b[3] & 0xFF |
- (b[2] & 0xFF) << 8 |
- (b[1] & 0xFF) << 16 |
- (b[0] & 0xFF) << 24;
- }
- public static byte[] intToByteArray(int a) {
- return new byte[] {
- (byte) ((a >> 24) & 0xFF),
- (byte) ((a >> 16) & 0xFF),
- (byte) ((a >> 8) & 0xFF),
- (byte) (a & 0xFF)
- };
- }
- //byte 数组与 long 的相互转换
- public static byte[] longToBytes(long x) {
- buffer.putLong(0, x);
- return buffer.array();
- }
- public static long bytesToLong(byte[] bytes) {
- buffer.put(bytes, 0, bytes.length);
- buffer.flip();//need flip
- return buffer.getLong();
- }
* 将short转成byte[2]
*
* @param a
* @return
*/
public static byte[] short2Byte(short a)
{
byte[] b = new byte[2];
b[0] = (byte) (a >> 8);
b[1] = (byte) (a);
return b;
}
/**
* 将short转成byte[2]
*
* @param a
* @param b
* @param offset b中的偏移量
*/
public static void short2Byte(short a, byte[] b, int offset)
{
b[offset] = (byte) (a >> 8);
b[offset + 1] = (byte) (a);
}
/**
* 字符串转换成十六进制字符串
*
* @param str 待转换的ASCII字符串
* @return String 每个Byte之间空格分隔,如: [61 6C 6B]
*/
public static String str2HexStr(String str)
{
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder("");
byte[] bs = str.getBytes();
int bit;
for (int i = 0; i < bs.length; i++)
{
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f;
sb.append(chars[bit]);
sb.append(' ');
}
return sb.toString().trim();
}
/**
* 十六进制转换字符串
*
* @param hexStr Byte字符串(Byte之间无分隔符 如:[616C6B])
* @return String 对应的字符串
*/
public static String hexStr2Str(String hexStr)
{
String str = "0123456789ABCDEF";
char[] hexs = hexStr.toCharArray();
byte[] bytes = new byte[hexStr.length() / 2];
int n;
for (int i = 0; i < bytes.length; i++)
{
n = str.indexOf(hexs[2 * i]) * 16;
n += str.indexOf(hexs[2 * i + 1]);
bytes[i] = (byte) (n & 0xff);
}
return new String(bytes);
}
/**
* bytes转换成十六进制字符串
*
* @param b byte数组
* @return String 每个Byte值之间空格分隔
*/
public static String byte2HexStr(byte[] b)
{
String stmp = "";
StringBuilder sb = new StringBuilder("");
for (int n = 0; n < b.length; n++)
{
stmp = Integer.toHexString(b[n] & 0xFF);
sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
}
return sb.toString().toUpperCase().trim();
}
/**
* bytes字符串转换为Byte值
*
* @param src Byte字符串,每个Byte之间没有分隔符
* @return byte[]
*/
public static byte[] hexStr2Bytes(String src)
{
String hexString = src.toLowerCase();
final byte[] byteArray = new byte[hexString.length() / 2];
int k = 0;
for (int i = 0; i < byteArray.length; i++)
{
// 转换成字节需要两个16进制的字符,高位在先
// 将hex 转换成byte "&" 操作为了防止负数的自动扩展
// hex转换成byte 其实只占用了4位,然后把高位进行右移四位
// 然后“|”操作 低四位 就能得到 两个 16进制数转换成一个byte.
byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
byteArray[i] = (byte) (high << 4 | low);
k += 2;
}
return byteArray;
}
/**
* bytes转换成十六进制字符串0补FF
*
* @param b byte数组
* @return String 每个Byte值之间空格分隔
*/
public static String byte2HexStr2(byte[] b)
{
String stmp = "";
StringBuilder sb = new StringBuilder("");
for (int n = 0; n < b.length; n++)
{
stmp = Integer.toHexString(b[n] & 0xFF);
if("0".equals(stmp)){
sb.append("FF");
}else {
sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
}
}
return sb.toString().toUpperCase().trim();
}
/**
* bytes转换成十六进制字符串不补0
*
* @param b byte数组
* @return String 每个Byte值之间空格分隔
*/
public static String byte2HexStr1(byte[] b)
{
String stmp = "";
StringBuilder sb = new StringBuilder("");
for (int n = 0; n < b.length; n++)
{
stmp = Integer.toHexString(b[n] & 0xFF);
sb.append((stmp.length() == 1) ? "" + stmp : stmp);
}
return sb.toString().toUpperCase().trim();
}
/**
* bytes转换成十进制字符串
*
* @param b byte数组
* @return String 每个Byte值之间空格分隔
*/
public static String byte2DecimalStr(byte[] b)
{
String stmp = "";
StringBuilder sb = new StringBuilder("");
for (int n = 0; n < b.length; n++)
{
stmp = Integer.toString(b[n] & 0xFF);
sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
}
return sb.toString().toUpperCase().trim();
}