snoopy7713 发表于 2013-2-3 10:33:14

java整型数与网络字节序的 byte[] 数组转换关系

工作项目需要在java和c/c++之间进行socket通信,socket通信是以字节流或者字节包进行的,socket发送方须将数据转换为字节流或者字节包,而接收方则将字节流和字节包再转换回相应的数据类型。如果发送方和接收方都是同种语言,则一般只涉及到字节序的调整。而对于java和c/c++的通信,则情况就要复杂一些,主要是因为java中没有unsigned类型,并且java和c在某些数据类型上的长度不一致。
    本文就是针对这种情况,整理了java数据类型和网络字节流或字节包(相当于java的byte数组)之间转换方法。实际上网上这方面的资料不少,但往往不全,甚至有些有错误,于是就花了点时间对java整型数和网络字节序的byte[]之间转换的各种情况做了一些验证和整理。整理出来的函数如下:


public class ByteConvert {/** * 长整形转byte数组 ** @param n *            长整形数字 * @return 转换后的数组 */public static byte[] longToBytes(long n) {byte[] b = new byte;b = (byte) (n & 0xff);b = (byte) (n >> 8 & 0xff);b = (byte) (n >> 16 & 0xff);b = (byte) (n >> 24 & 0xff);b = (byte) (n >> 32 & 0xff);b = (byte) (n >> 40 & 0xff);b = (byte) (n >> 48 & 0xff);b = (byte) (n >> 56 & 0xff);return b;}/** * 长整形转byte数组 ** @param n *            长整形数字 * @param array *            转换后的结果 * @param offset *            从第offset位开始转换 */public static void longToBytes(long n, byte[] array, int offset) {array = (byte) (n & 0xff);array = (byte) (n >> 8 & 0xff);array = (byte) (n >> 16 & 0xff);array = (byte) (n >> 24 & 0xff);array = (byte) (n >> 32 & 0xff);array = (byte) (n >> 40 & 0xff);array = (byte) (n >> 48 & 0xff);array = (byte) (n >> 56 & 0xff);}/** * bytes 转 Long ** @param array *            要转换的byte * @return long长整形数字 */public static long bytesToLong(byte[] array) {return ((((long) array & 0xff) << 56) | (((long) array & 0xff) << 48) | (((long) array & 0xff) << 40)      | (((long) array & 0xff) << 32) | (((long) array & 0xff) << 24)      | (((long) array & 0xff) << 16) | (((long) array & 0xff) << 8) | (((long) array & 0xff) << 0));}/** * byte数组转长整形数字 ** @param array *            要转换的byte数组 * @param offset *            从第offset开始转换 * @return 转换后的长整形数字 */public static long bytesToLong(byte[] array, int offset) {return ((((long) array & 0xff) << 56) | (((long) array & 0xff) << 48)      | (((long) array & 0xff) << 40) | (((long) array & 0xff) << 32)      | (((long) array & 0xff) << 24) | (((long) array & 0xff) << 16)      | (((long) array & 0xff) << 8) | (((long) array & 0xff) << 0));}public static byte[] intToBytes(int n) {byte[] b = new byte;b = (byte) (n & 0xff);b = (byte) (n >> 8 & 0xff);b = (byte) (n >> 16 & 0xff);b = (byte) (n >> 24 & 0xff);return b;}public static void intToBytes(int n, byte[] array, int offset) {array = (byte) (n & 0xff);array = (byte) (n >> 8 & 0xff);array = (byte) (n >> 16 & 0xff);array = (byte) (n >> 24 & 0xff);}/** * @param b * @return */public static int bytesToInt(byte b[]) {return b & 0xff | (b & 0xff) << 8 | (b & 0xff) << 16 | (b & 0xff) << 24;}/** * byte 数组转 int ** @param b *            byte数组 * @param offset *            从数组的第几位开始转 * @return 整形 */public static int bytesToInt(byte b[], int offset) {return b & 0xff | (b & 0xff) << 8 | (b & 0xff) << 16      | (b & 0xff) << 24;}/** * 无符号整形转数组 ** @param n *            要转换的整形 * @return byte数组 */public static byte[] uintToBytes(long n) {byte[] b = new byte;b = (byte) (n & 0xff);b = (byte) (n >> 8 & 0xff);b = (byte) (n >> 16 & 0xff);b = (byte) (n >> 24 & 0xff);return b;}public static void uintToBytes(long n, byte[] array, int offset) {array = (byte) (n);array = (byte) (n >> 8 & 0xff);array = (byte) (n >> 16 & 0xff);array = (byte) (n >> 24 & 0xff);}public static long bytesToUint(byte[] array) {return ((long) (array & 0xff)) | ((long) (array & 0xff)) << 8 | ((long) (array & 0xff)) << 16      | ((long) (array & 0xff)) << 24;}public static long bytesToUint(byte[] array, int offset) {return ((long) (array & 0xff)) | ((long) (array & 0xff)) << 8      | ((long) (array & 0xff)) << 16 | ((long) (array & 0xff)) << 24;}public static byte[] shortToBytes(short n) {byte[] b = new byte;b = (byte) (n & 0xff);b = (byte) ((n >> 8) & 0xff);return b;}public static void shortToBytes(short n, byte[] array, int offset) {array = (byte) (n & 0xff);array = (byte) ((n >> 8) & 0xff);}public static short bytesToShort(byte[] b) {return (short) (b & 0xff | (b & 0xff) << 8);}public static short bytesToShort(byte[] b, int offset) {return (short) (b & 0xff | (b & 0xff) << 8);}public static byte[] ushortToBytes(int n) {byte[] b = new byte;b = (byte) (n & 0xff);b = (byte) ((n >> 8) & 0xff);return b;}public static void ushortToBytes(int n, byte[] array, int offset) {array = (byte) (n & 0xff);array = (byte) ((n >> 8) & 0xff);}public static int bytesToUshort(byte b[]) {return b & 0xff | (b & 0xff) << 8;}public static int bytesToUshort(byte b[], int offset) {return b & 0xff | (b & 0xff) << 8;}public static byte[] ubyteToBytes(int n) {byte[] b = new byte;b = (byte) (n & 0xff);return b;}public static void ubyteToBytes(int n, byte[] array, int offset) {array = (byte) (n & 0xff);}public static int bytesToUbyte(byte[] array) {return array & 0xff;}public static int bytesToUbyte(byte[] array, int offset) {return array & 0xff;}// char 类型、 float、double 类型和 byte[] 数组之间的转换关系还需继续研究实现。}
页: [1]
查看完整版本: java整型数与网络字节序的 byte[] 数组转换关系