| | |
| | | numberHex = String.format("%2s", numberHex).replace(' ', '0'); |
| | | return numberHex; |
| | | } |
| | | |
| | | /** |
| | | * 从byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序,和和intToBytes()配套使用 |
| | | * |
| | | * @param src: byte数组 |
| | | * @param offset: 从数组的第offset位开始 |
| | | * @return int数值 |
| | | */ |
| | | public static int bytesToIntDesc(byte[] src, int offset) { |
| | | int value=0; |
| | | int length = src.length; |
| | | for(int i=0;i<length;i++){ |
| | | value+=(int)((src[offset+i]&0xFF)<<(length-i-1)*8); |
| | | } |
| | | return value; |
| | | } |
| | | /** |
| | | * 将int数值转换为占size个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序。 和bytesToInt()配套使用 |
| | | * @param value |
| | | * 要转换的int值 |
| | | * @return byte数组 |
| | | */ |
| | | public static byte[] intToBytesDesc( int value,int size ) |
| | | { |
| | | byte[] src = new byte[size]; |
| | | for(int i=0;i<size;i++){ |
| | | src[i] = (byte) ((value>>(size-i-1)*8) & 0xFF); |
| | | } |
| | | return src; |
| | | } |
| | | /** |
| | | * 从byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序,和和intToBytes()配套使用 |
| | | * |
| | |
| | | * @return int数值 |
| | | */ |
| | | public static int bytesToInt(byte[] src, int offset) { |
| | | int value; |
| | | value = (int) ((src[offset] & 0xFF) |
| | | | ((src[offset+1] & 0xFF)<<8) |
| | | | ((src[offset+2] & 0xFF)<<16) |
| | | | ((src[offset+3] & 0xFF)<<24)); |
| | | int value=0; |
| | | for(int i=0;i<src.length;i++){ |
| | | value+=(int)((src[offset+i]&0xFF)<<i*8); |
| | | } |
| | | return value; |
| | | } |
| | | /** |
| | | * 将int数值转换为占size个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序。 和bytesToInt()配套使用 |
| | | * @param value |
| | | * 要转换的int值 |
| | | * @return byte数组 |
| | | */ |
| | | public static byte[] intToBytes( int value,int size ) |
| | | { |
| | | byte[] src = new byte[size]; |
| | | for(int i=0;i<src.length;i++){ |
| | | src[i] = (byte) ((value>>i*8) & 0xFF); |
| | | } |
| | | return src; |
| | | } |
| | | } |