| | |
| | | */ |
| | | public static byte[] hexStr2Bytes(String src) |
| | | { |
| | | src = src.replace(" ",""); |
| | | int m=0,n=0; |
| | | int l=src.length()/2; |
| | | System.out.println(l); |
| | |
| | | } |
| | | |
| | | /** |
| | | * short转为字节数组 |
| | | * short转为字节数组 大端 |
| | | * @param data |
| | | * @return 包含2个字节的字节数组 |
| | | */ |
| | |
| | | byte[] bytes = new byte[2]; |
| | | bytes[1] = (byte) (data & 0xff); |
| | | bytes[0] = (byte) ((data & 0xff00) >> 8); |
| | | return bytes; |
| | | } |
| | | |
| | | /** |
| | | * 大端 int 转成 4个字节的byte数组 |
| | | * @param data |
| | | * @return |
| | | */ |
| | | public static byte[] intGetBytes(int data) { |
| | | byte[] bytes = new byte[4]; |
| | | bytes[3] = (byte) ((data & 0xff)); |
| | | bytes[2] = (byte) ((data & 0xff00) >> 8); |
| | | bytes[1] = (byte) ((data & 0xff0000) >> 16); |
| | | bytes[0] = (byte) ((data & 0xff000000) >> 24); |
| | | return bytes; |
| | | } |
| | | |
| | |
| | | System.arraycopy(shortGetBytes((short)3),0,messageBody,22,2); |
| | | System.out.println(byte2HexStr(messageBody)); |
| | | |
| | | |
| | | byte[] bytes =new byte[]{(byte) 0x80}; |
| | | short aShort= getShort(bytes); |
| | | System.out.println(aShort); |
| | | |
| | | } |
| | | |
| | | public static byte[] subArray(byte[] srcBytes, int begin, int length) { |
| | |
| | | Log.i(TAG, "subArray: "+byte2HexStr(bytes)); |
| | | return bytes; |
| | | } |
| | | |
| | | public static short getShort(byte[] bytes) { |
| | | short s =0; |
| | | if (bytes.length==1){ |
| | | s|=(bytes[0]&0xff); |
| | | return s; |
| | | } |
| | | s =(short) ((0xff & bytes[1]) | (0xff00 & (bytes[0] << 8))); |
| | | Log.i(TAG, "getShort: "+s); |
| | | return s ; |
| | | } |
| | | } |