您的位置:首页 > 编程语言 > Java开发

Java基本类型转byte[],java与c通信数据转换

2015-09-11 09:37 585 查看
在进行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();

ll = 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();

ll = 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();

ll = 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();

ll = 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流数据转成需要的数据呢。这里用到了共同体。

[html]

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流时。

[html]

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型数据。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: