Java基本類型轉byte[],java與c通信數據轉換

  1. 在進行java編程是有時需要進行 基本類型到byte[]數據的轉化。在進行與c和C++的通信時我們有時也需要將float,long,int,char等數據轉換成byte通過socket通信
  2. 等發送到C或C++,然後C和C++再將byte[]轉換成float,long,int。
  3. 下面這個類是個工具類,除最後兩個方法僅能用在java和java通信使用,其它可以用作與c進行通信時轉換數據使用。
  4. import java.nio.ByteBuffer;
  5. import java.nio.FloatBuffer;
  6. public class TypeUtils {
  7. public static byte[] int2Byte(int l) {
  8. byte[] b = new byte[4];
  9. for (int i = 0; i < b.length; i++) {
  10. b[i] = new Integer(l).byteValue();
  11. l = l >> 8;
  12. }
  13. return b;
  14. }
  15. public static int byte2Int(byte[] b) {
  16. int l = 0;
  17. l = b[0];
  18. l &= 0xff;
  19. l |= ((int) b[1] << 8);
  20. l &= 0xffff;
  21. l |= ((int) b[2] << 16);
  22. l &= 0xffffff;
  23. l |= ((int) b[3] << 24);
  24. l &= 0xffffffff;
  25. return l;
  26. }
  27. public static byte[] longToByte(long l) {
  28. byte[] b = new byte[8];
  29. for (int i = 0; i < b.length; i++) {
  30. b[i] = new Long(l).byteValue();
  31. l = l >> 8;
  32. }
  33. return b;
  34. }
  35. public static long byteToLong(byte[] b) {
  36. long l = 0;
  37. l |= (((long) b[7] & 0xff) << 56);
  38. l |= (((long) b[6] & 0xff) << 48);
  39. l |= (((long) b[5] & 0xff) << 40);
  40. l |= (((long) b[4] & 0xff) << 32);
  41. l |= (((long) b[3] & 0xff) << 24);
  42. l |= (((long) b[2] & 0xff) << 16);
  43. l |= (((long) b[1] & 0xff) << 8);
  44. l |= ((long) b[0] & 0xff);
  45. return l;
  46. }
  47. public static byte[] float2Byte(float f) {
  48. byte[] b = new byte[4];
  49. int l = Float.floatToIntBits(f);
  50. for (int i = 0; i < b.length; i++) {
  51. b[i] = new Integer(l).byteValue();
  52. l = l >> 8;
  53. }
  54. return b;
  55. }
  56. public static float byte2Float(byte[] b) {
  57. int l = 0;
  58. l = b[0];
  59. l &= 0xff;
  60. l |= ((int) b[1] << 8);
  61. l &= 0xffff;
  62. l |= ((int) b[2] << 16);
  63. l &= 0xffffff;
  64. l |= ((int) b[3] << 24);
  65. l &= 0xffffffffl;
  66. return Float.intBitsToFloat(l);
  67. }
  68. public static byte[] doubleToByte(double d) {
  69. byte[] b = new byte[8];
  70. long l = Double.doubleToLongBits(d);
  71. for (int i = 0; i < b.length; i++) {
  72. b[i] = new Long(l).byteValue();
  73. l = l >> 8;
  74. }
  75. return b;
  76. }
  77. public static char[] bytesToChars(byte[] bytes,int offset, int count) {
  78. char chars[] = new char[count];
  79. for(int i = 0;i< count;i++){
  80. chars[i] = (char)bytes[i];
  81. }
  82. return chars;
  83. }
  84. public static byte[] charsToBytes(char[] chars,int offset,int count) {
  85. byte bytes[] = new byte[count];
  86. for(int i = 0;i< count;i++){
  87. bytes[i] = (byte)chars[i];
  88. }
  89. return bytes;
  90. }
  91. public static byte[] floatToByte(float v) {
  92. ByteBuffer bb = ByteBuffer.allocate(4);
  93. byte[] ret = new byte[4];
  94. FloatBuffer fb = bb.asFloatBuffer();
  95. fb.put(v);
  96. bb.get(ret);
  97. return ret;
  98. }
  99. public static float byteToFloat(byte[] v) {
  100. ByteBuffer bb = ByteBuffer.wrap(v);
  101. FloatBuffer fb = bb.asFloatBuffer();
  102. return fb.get();
  103. }
  104. }
在進行java編程是有時需要進行 基本類型到byte[]數據的轉化。在進行與c和C++的通信時我們有時也需要將float,long,int,char等數據轉換成byte通過socket通信
等發送到C或C++,然後C和C++再將byte[]轉換成float,long,int。
下面這個類是個工具類,除最後兩個方法僅能用在java和java通信使用,其它可以用作與c進行通信時轉換數據使用。

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

public class TypeUtils {

	public static byte[] int2Byte(int l) {
		byte[] b = new byte[4];
		for (int i = 0; i < b.length; i++) {
			b[i] = new Integer(l).byteValue();
			l = l >> 8;
		}
		return b;
	}

	public static int byte2Int(byte[] b) {
        int l = 0;
        l = b[0];
        l &= 0xff;
        l |= ((int) b[1] << 8);
        l &= 0xffff;
        l |= ((int) b[2] << 16);
        l &= 0xffffff;
        l |= ((int) b[3] << 24);
        l &= 0xffffffff;
        return l;
    }


	public static byte[] longToByte(long l) {
		byte[] b = new byte[8];
		for (int i = 0; i < b.length; i++) {
			b[i] = new Long(l).byteValue();
			l = l >> 8;
		}
		return b;
	}

    public static long byteToLong(byte[] b) {
        long l = 0;
        l |= (((long) b[7] & 0xff) << 56);
        l |= (((long) b[6] & 0xff) << 48);
        l |= (((long) b[5] & 0xff) << 40);
        l |= (((long) b[4] & 0xff) << 32);
        l |= (((long) b[3] & 0xff) << 24);
        l |= (((long) b[2] & 0xff) << 16);
        l |= (((long) b[1] & 0xff) << 8);
        l |= ((long) b[0] & 0xff);
        return l;
    }


	public static byte[] float2Byte(float f) {
		byte[] b = new byte[4];
		int l = Float.floatToIntBits(f);
		for (int i = 0; i < b.length; i++) {
			b[i] = new Integer(l).byteValue();
			l = l >> 8;
		}
		return b;
	}

	public static float byte2Float(byte[] b) {
        int l = 0;
        l = b[0];
        l &= 0xff;
        l |= ((int) b[1] << 8);
        l &= 0xffff;
        l |= ((int) b[2] << 16);
        l &= 0xffffff;
        l |= ((int) b[3] << 24);
        l &= 0xffffffffl;
        return Float.intBitsToFloat(l);
    }

	public static byte[] doubleToByte(double d) {
		byte[] b = new byte[8];
		long l = Double.doubleToLongBits(d);
		for (int i = 0; i < b.length; i++) {
			b[i] = new Long(l).byteValue();
			l = l >> 8;
		}
		return b;
	}



    public static char[] bytesToChars(byte[] bytes,int offset, int count) {
        char chars[] = new char[count];
        for(int i = 0;i< count;i++){
            chars[i] = (char)bytes[i];
        }
        return chars;
    }

    public static byte[] charsToBytes(char[] chars,int offset,int count) {
        byte bytes[] = new byte[count];
        for(int i = 0;i< count;i++){
            bytes[i] = (byte)chars[i];
        }
        return bytes;
    }

    public static byte[] floatToByte(float v) {
        ByteBuffer bb = ByteBuffer.allocate(4);
        byte[] ret = new byte[4];
        FloatBuffer fb = bb.asFloatBuffer();
        fb.put(v);
        bb.get(ret);
        return ret;
    }

    public static float byteToFloat(byte[] v) {
        ByteBuffer bb = ByteBuffer.wrap(v);
        FloatBuffer fb = bb.asFloatBuffer();
        return fb.get();
    }
}


c和C++如何實現將java傳來的byte流數據轉成需要的數據呢。這裏用到了共同體。

  1. static union FloatValue{
  2. char val[4];
  3. float f;
  4. int i;
  5. } mf_t;
  6. static union LongValue{
  7. char val[8];
  8. long long l;
  9. } ml_t;
static union FloatValue{
char val[4];
float f;
int i;
} mf_t;

static union LongValue{
char val[8];
long long l;
} ml_t;

java的byte流在c語言裏表現爲char*,對於32位機器來說,int和float均爲4個字節長。java中的long對應 c中的long long ,8個字節。

以下代碼不完整,需自己補充完整。

假如我們督導java傳過來的byte數據放在char* buf 中。

當buf中存放的是float byte流時。

  1. mf_t.val[0]= buf[0];
  2. mf_t.val[1]= buf[1];
  3. mf_t.val[2] = buf[2];
  4. mf_t.val[3] = buf [3];
  5. 此時讀取mf_t.f的值就是java想傳給我們的float值了。主要是利用了共同題的特性。
  6. 這裏buf中保存了4個byte數據。如果buf中的數據是int,mf_t.i就是需要的int值了。
  7. 類似這種操作利用上面的ml_t也同樣可以得到long型數據。
mf_t.val[0]= buf[0];

mf_t.val[1]= buf[1];

mf_t.val[2] = buf[2];

mf_t.val[3] = buf [3];

此時讀取mf_t.f的值就是java想傳給我們的float值了。主要是利用了共同題的特性。
這裏buf中保存了4個byte數據。如果buf中的數據是int,mf_t.i就是需要的int值了。

類似這種操作利用上面的ml_t也同樣可以得到long型數據。







發佈了94 篇原創文章 · 獲贊 310 · 訪問量 171萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章