- 在進行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();
- }
- }
在進行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流數據轉成需要的數據呢。這裏用到了共同體。
- static union FloatValue{
- char val[4];
- float f;
- int i;
- } mf_t;
- static union LongValue{
- char val[8];
- long long l;
- } 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流時。
- 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型數據。
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型數據。