- 在寫通訊程序時,有時需要解析二進制數據流,可能會遇到java數值類型與二進制byte數組的類型轉換問題。因此,筆者提供下面代碼實例,供大家需要時參考。
- import java.io.*;
- /**
- * java數值類型與二進制類型相互轉換
- * @author lijun
- *
- */
- public class MyTest {
- /**
- * int32轉換爲二進制(4個字節)
- * @param i 待轉換的整數
- * @return 返回4字節二進制數
- */
- public byte[] int2byte(int i){
- byte[] res = new byte[4];
- res[3] = (byte)i;
- res[2] = (byte)(i>>>8);
- res[1] = (byte)(i>>>16);
- res[0] = (byte)(i>>>24);
- return res;
- }
- /**
- * 4字節二進制數轉換爲int32的整數
- * @param bytes 4字節的二進制數
- * @return int32整數
- */
- public int byte2int(byte[] bytes){
- int res = (((bytes[0]<<24)>>>24)<<24)|(((bytes[1]<<24)>>>24)<<16)|(((bytes[2]<<24)>>>24)<<8)|((bytes[3]<<24)>>>24);
- return res;
- }
- /**
- * int16整數轉換二2字節的二進制數
- * @param i int16整數
- * @return 2字節的二進制數
- */
- public byte[] int16Tobyte(int i){
- byte[] res = new byte[2];
- res[1] = (byte)i;
- res[0] = (byte)(i>>>8);
- return res;
- }
- /**
- * 2字節的二進制數轉換爲int16整數
- * @param bytes 2字節的二進制數
- * @return int16整數
- */
- public int byteToint16(byte[] bytes){
- int res = ((bytes[0]<<8)|((bytes[1]<<24)>>>24));
- return res;
- }
- /**
- * 長整型long轉換爲8字節的二進制數
- * @param l 長整型long
- * @return 8字節的二進制數
- */
- public byte[] long2byte(long l){
- byte[] res = new byte[8];
- res[7] = (byte)l;
- res[6] = (byte)(l>>>8);
- res[5] = (byte)(l>>>16);
- res[4] = (byte)(l>>>24);
- res[3] = (byte)(l>>>32);
- res[2] = (byte)(l>>>40);
- res[1] = (byte)(l>>>48);
- res[0] = (byte)(l>>>56);
- return res;
- }
- /**
- * 8字節的二進制數轉換爲長整型long
- * @param bytes 8字節的二進制數
- * @return 長整型long
- */
- public long byte2long(byte[] bytes){
- long l0 = bytes[0];
- long l1 = bytes[1];
- long l2 = bytes[2];
- long l3 = bytes[3];
- long l4 = bytes[4];
- long l5 = bytes[5];
- long l6 = bytes[6];
- long l7 = bytes[7];
- long res = (l0<<56)|(((l1<<56)>>>56)<<48)|(((l2<<56)>>>56)<<40)|(((l3<<56)>>>56)<<32)|(((l4<<56)>>>56)<<24)|(((l5<<56)>>>56)<<16)|(((l6<<56)>>>56)<<8)|((l7<<56)>>>56);
- return res;
- }
- /**
- * 浮點型float轉換爲4字節的二進制數
- * @param f 浮點數float
- * @return 4字節的二進制數
- */
- public byte[] float2byte(float f){
- byte[] res = new byte[4];
- int l = Float.floatToIntBits(f);
- for(int i=3; i>=0; i--){
- res[i] = new Integer(l).byteValue();
- l >>= 8;
- }
- return res;
- }
- /**
- * 4字節的二進制數轉換爲浮點數float
- * @param bytes 4字節的二進制數
- * @return 浮點數float
- */
- public float byte2float(byte[] bytes){
- int l = byte2int(bytes);
- float res = Float.intBitsToFloat(l);
- return res;
- }
- /**
- * 雙浮點數轉換爲8字節的二進制數
- * @param d 雙浮點數double
- * @return 8字節的二進制數
- */
- public byte[] double2byte(double d){
- long l = Double.doubleToLongBits(d);
- byte[] res = long2byte(l);
- return res;
- }
- /**
- * 8字節的二進制數轉換爲雙浮點數
- * @param bytes 8字節的二進制數
- * @return 雙浮點數double
- */
- public double byte2double(byte[] bytes){
- long l = byte2long(bytes);
- double res = Double.longBitsToDouble(l);
- return res;
- }
- public void test()throws Exception{
- FileOutputStream fos = new FileOutputStream("d:/data.dat");
- java.io.DataOutputStream dos = new DataOutputStream(fos);
- double d = 233344.233444;
- dos.writeDouble(d);
- dos.close();
- fos.close();
- }
- public static void main(String[] args)throws Exception{
- System.out.println("begin write a int type to file:'c:/data.dat'");
- new MyTest().test();
- System.out.println("end write..");
- }
- }
- 以下爲單元測試代碼:
- import junit.framework.Assert;
- import org.junit.Test;
- public class MyTestTest {
- private MyTest test = new MyTest();
- @Test
- public void int2byteTest()
- {
- int i = -10101;
- byte[] bytes = test.int2byte(i);
- int j = test.byte2int(bytes);
- Assert.assertEquals(i, j);
- i = 10101;
- bytes = test.int2byte(i);
- j = test.byte2int(bytes);
- Assert.assertEquals(i, j);
- i = -0x1f1f1f;
- bytes = test.int2byte(i);
- j = test.byte2int(bytes);
- Assert.assertEquals(i, j);
- i = 0x1f1f1f;
- bytes = test.int2byte(i);
- j = test.byte2int(bytes);
- Assert.assertEquals(i, j);
- i = 0x1f1f1f1f;
- bytes = test.int2byte(i);
- j = test.byte2int(bytes);
- Assert.assertEquals(i, j);
- i = -0x1f1f1f1f;
- bytes = test.int2byte(i);
- j = test.byte2int(bytes);
- Assert.assertEquals(i, j);
- }
- @Test
- public void int16TobyteTest(){
- int i = -0x01;
- byte[] bytes = test.int16Tobyte(i);
- int j = test.byteToint16(bytes);
- Assert.assertEquals("-0x01",i, j);
- i = 0x01;
- bytes = test.int16Tobyte(i);
- j = test.byteToint16(bytes);
- Assert.assertEquals("0x01",i, j);
- i = -0x0101;
- bytes = test.int16Tobyte(i);
- j = test.byteToint16(bytes);
- Assert.assertEquals("-0x0101",i, j);
- i = 0x0101;
- bytes = test.int16Tobyte(i);
- j = test.byteToint16(bytes);
- Assert.assertEquals("0x0101",i, j);
- }
- @Test
- public void long2byteTest(){
- long l = -0x01;
- byte[] bytes = test.long2byte(l);
- long j = test.byte2long(bytes);
- Assert.assertEquals("-0x01",l, j);
- l = 0x01;
- bytes = test.long2byte(l);
- j = test.byte2long(bytes);
- Assert.assertEquals("0x01",l, j);
- l = -0x0101;
- bytes = test.long2byte(l);
- j = test.byte2long(bytes);
- Assert.assertEquals("-0x0101",l, j);
- l = -0x0101;
- bytes = test.long2byte(l);
- j = test.byte2long(bytes);
- Assert.assertEquals("-0x0101",l, j);
- }
- @Test
- public void float2byteTest()
- {
- float f = 233344.233444f;
- byte[] bytes = test.float2byte(f);
- float j = test.byte2float(bytes);
- Assert.assertEquals(f, j);
- }
- @Test
- public void double2byteTest(){
- double d = 233344.233444;
- byte[] bytes = test.double2byte(d);
- double j = test.byte2double(bytes);
- Assert.assertEquals(d, j);
- }
- }
java byte 與 binary 轉換
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.