package com.rjtx.archiver.test; import org.junit.Test; public class TestConverter { @Test public void test1(){ byte[] arr = int2Bytes(1555) ; for(byte by : arr){ System.out.println(by) ; } System.out.println(bytes2Int(arr)) ; } /** * 将整数转换成字节数组 */ public byte[] int2Bytes(int i){ byte[] arr = new byte[4]; arr[0] = (byte)i ; arr[1] = (byte)(i >> 8) ; arr[2] = (byte)(i >> 16) ; arr[3] = (byte)(i >> 24) ; return arr ; } /** * 将字节数组转换成整数 */ public int bytes2Int(byte[] bytes){ int i0 = bytes[0] ; int i1 = (bytes[1] & 0xFF) << 8 ; //补字节生成32位字节 System.out.println("bytes[1]:"+bytes[1]); System.out.println("0xFF:"+0xFF); System.out.println("bytes[1] & 0xFF:"+(bytes[1] & 0xFF)); System.out.println("i1:"+i1); int i2 = (bytes[2] & 0xFF) << 16 ; int i3 = (bytes[3] & 0xFF) << 24 ; return i0 | i1 | i2 | i3 ; } }
整数转换成字节数组再由字节数组转换成整数的方法
最新推荐文章于 2024-07-02 11:00:21 发布