在编写网络通讯程序的时候,经常会遇到网络字节顺序的转换,以下是java网络顺序的转换类,下面还有java与c 通讯时的一个转换类(java与c的网络字节顺序是相反的)
package org.prl;
public class TypeConvert {
public TypeConvert() {
}
public static int byte2int(byte b[], int offset) {
return b[offset + 3] & 0xff | (b[offset + 2] & 0xff) << 8
| (b[offset + 1] & 0xff) << 16 | (b[offset] & 0xff) << 24;
}
public static int byte2int(byte b[]) {
return b[3] & 0xff | (b[2] & 0xff) << 8 | (b[1] & 0xff) << 16
| (b[0] & 0xff) << 24;
}
public static long byte2long(byte b[]) {
return (long) b[7] & (long) 255 | ((long) b[6] & (long) 255) << 8
| ((long) b[5] & (long) 255) << 16
| ((long) b[4] & (long) 255) << 24
| ((long) b[3] & (long) 255) << 32
| ((long) b[2] & (long) 255) << 40
| ((long) b[1] & (long) 255) << 48 | (long) b[0] << 56;
}
public static long byte2long(byte b[], int offset) {
return (long) b[offset + 7] & (long) 255
| ((long) b[offset + 6] & (long) 255) << 8
| ((long) b[offset + 5] & (long) 255) << 16
| ((long) b[offset + 4] & (long) 255) << 24
| ((long) b[offset + 3] & (long) 255) << 32
| ((long) b[offset + 2] & (long) 255) << 40
| ((long) b[offset + 1] & (long) 255) << 48
| (long) b[offset] << 56;
}
public static byte[] int2byte(int n) {
byte b[] = new byte[4];
b[0] = (byte) (n >> 24);
b[1] = (byte) (n >> 16);
b[2] = (byte) (n >> 8);
b[3] = (byte) n;
return b;
}
public static void int2byte(int n, byte buf[], int offset) {
buf[offset] = (byte) (n >> 24);
buf[offset + 1] = (byte) (n >> 16);
buf[offset + 2] = (byte) (n >> 8);
buf[offset + 3] = (byte) n;
}
public static byte[] short2byte(int n) {
byte b[] = new byte[2];
b[0] = (byte) (n >> 8);
b[1] = (byte) n;
return b;
}
public static void short2byte(int n, byte buf[], int offset) {
buf[offset] = (byte) (n >> 8);
buf[offset + 1] = (byte) n;
}
public static byte[] long2byte(long n) {
byte b[] = new byte[8];
b[0] = (byte) (int) (n >> 56);
b[1] = (byte) (int) (n >> 48);
b[2] = (byte) (int) (n >> 40);
b[3] = (byte) (int) (n >> 32);
b[4] = (byte) (int) (n >> 24);
b[5] = (byte) (int) (n >> 16);
b[6] = (byte) (int) (n >> 8);
b[7] = (byte) (int) n;
return b;
}
public static void long2byte(long n, byte buf[], int offset) {
buf[offset] = (byte) (int) (n >> 56);
buf[offset + 1] = (byte) (int) (n >> 48);
buf[offset + 2] = (byte) (int) (n >> 40);
buf[offset + 3] = (byte) (int) (n >> 32);
buf[offset + 4] = (byte) (int) (n >> 24);
buf[offset + 5] = (byte) (int) (n >> 16);
buf[offset + 6] = (byte) (int) (n >> 8);
buf[offset + 7] = (byte) (int) n;
}
public static void byte2byte(byte[] s, byte[] buf, int offset) {
for (int i = 0; i < s.length; i++)
buf[offset + i] = s[i];
}
/**
* 将float转为低字节在前,高字节在后的byte数组
*/
private static byte[] toLH(float f) {
return toLH(Float.floatToRawIntBits(f));
}
}
java与c 通讯时的一个转换类
package org.prl;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
public class CTypeConvert {
public static byte[] int2byte(int n) {
byte[] b = new byte[4];
b[0] = (byte) (n & 0xff);
b[1] = (byte) (n >> 8 & 0xff);
b[2] = (byte) (n >> 16 & 0xff);
b[3] = (byte) (n >> 24 & 0xff);
return b;
}
public static float bytes2float(byte[] b) {
ByteBuffer bb = ByteBuffer.allocate(4);
for (int i = 3; i > -1; i--)
bb.put(b[i]);
try {
float f = bb.getFloat();
return f;
} catch (BufferUnderflowException e) {
e.printStackTrace();
return 0.0f;
}
}
public static byte[] float2bytes(float f) {
return int2bytes(Float.floatToRawIntBits(f));
}
public static int bytes2int(byte[] b) {
int s = 0;
for (int i = 3; i > -1; i--) {
s *= 256;
s += b[i] < 0 ? b[i] + 256 : b[i];
}
return s;
}
public static byte[] int2bytes(int n) {
byte[] b = new byte[4];
b[0] = (byte) (n & 0xff);
b[1] = (byte) (n >> 8 & 0xff);
b[2] = (byte) (n >> 16 & 0xff);
b[3] = (byte) (n >> 24 & 0xff);
return b;
}
public static String bytes2string(byte[] b, int length) {
return new String(b, 0, length);
}
public static byte[] string2bytes(String s, int length) {
while (s.getBytes().length < length)
s += "\0";
return s.getBytes();
}
}