Hex字符串转int
static public int HexToInt(String inHex)
{
return Integer.parseInt(inHex, 16);
}
Hex字符串转byte
static public byte HexToByte(String inHex)
{
return (byte)Integer.parseInt(inHex,16);
}
1字节转2个Hex字符
static public String Byte2Hex(Byte inByte)//1字节转2个Hex字符
{
return String.format("%02x", inByte).toUpperCase();
}
字节数组转转hex字符串
static public String ByteArrToHex(byte[] inBytArr)//字节数组转转hex字符串
{
StringBuilder strBuilder=new StringBuilder();
int j=inBytArr.length;
for (int i = 0; i < j; i++)
{
strBuilder.append(Byte2Hex(inBytArr[i]));
strBuilder.append(" ");
}
return strBuilder.toString();
}
以小端模式将int转成byte[]
/**
* 以小端模式将int转成byte[]
*
* @param value
* @return
*/
public static byte[] intToBytesLittle(int value) {
byte[] src = new byte[4];
src[3] = (byte) ((value >> 24) & 0xFF);
src[2] = (byte) ((value >> 16) & 0xFF);
src[1] = (byte) ((value >> 8) & 0xFF);
src[0] = (byte) (value & 0xFF);
return src;
}
以大端模式将int转成byte[]
/**
* 以大端模式将int转成byte[]
*/
public static byte[] intToBytesBig(int value) {
byte[] src = new byte[4];
src[0] = (byte) ((value >> 24) & 0xFF);
src[1] = (byte) ((value >> 16) & 0xFF);
src[2] = (byte) ((value >> 8) & 0xFF);
src[3] = (byte) (value & 0xFF);
return src;
}
list --> int[]
private static int[] list2int(List<Integer> list) {
if (list == null || list.size() < 0)
return null;
int[] ints = new int[list.size()];
int i = 0;
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
ints[i] = iterator.next();
i++;
}
return ints;
}
list --> byte[]
list<Byte> --> byte[]
private static byte[] list2byte(List<Byte> list) {
if (list == null || list.size() < 0)
return null;
byte[] bytes = new byte[list.size()];
int i = 0;
Iterator<Byte> iterator = list.iterator();
while (iterator.hasNext()) {
bytes[i] = iterator.next();
i++;
}
return bytes;
}
byte[] --> int[]
public static int[] bytes2Int(byte[] bytes) {
if (bytes == null || bytes.length < 1) {
return null;
}
int[] ints = new int[bytes.length];
for (int i = 0; i < bytes.length; i++) {
ints[i] = bytes[i] < 0 ? bytes[i] & 255 : bytes[i];
}
return ints;
}
byte --> int
public static int byte2Int(byte b) {
return b < 0 ? b & 255 : b;
}