Java基本类型转byte[],java与c通信数据转换
来源:互联网 发布:80端口被进程4占用 编辑:程序博客网 时间:2024/06/05 09:32
在进行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++,然后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 0
- Java基本类型转byte[],java与c通信数据转换
- Java基本类型转byte[],java与c通信数据转换
- Java基本类型转byte[],java与c通信数据转换
- java 基本类型与byte[]相互转换
- Java基本类型与byte数组之间相互转换
- Java基本类型与byte数组之间相互转换
- Java基本类型与byte数组之间相互转换
- Java基本类型与byte数组之间相互转换
- Java基本类型与byte数组之间相互转换
- Java基本类型与byte数组之间相互转换
- Java byte类型转换
- Java基本类型与类型转换(2)——位操作: byte & 0xff
- Java 记录java基本类型与Byte数组之间相互转换
- java &安卓 16进制字符串与byte互换以及int与byte类型数据转换
- java int 类型与 byte 类型之间的转换
- java int 类型与 byte 类型之间的转换
- Java类型转换(int - byte[],float - byte[])
- Java类型转换(int - byte[],float - byte[])
- java 汉字保存到mysql 乱码
- XDoclet
- linux文件类型
- 忘记MySql 5.X root 密码 windows
- X64编程总结
- Java基本类型转byte[],java与c通信数据转换
- Gradle在eclipse内执行编译和运行慢的问题解决
- C++中引用和指针的区别
- 项目3——体验复杂度 (1)
- 7 bad habits of highly ineffective software engineers
- 第二周 项目2 程序的多文件组织
- IOS 获取APP当前的版本号
- Java Map 按key排序和按Value排序
- LeetCode Climbing Stairs