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

在进行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型数据。   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值