美文网首页
Socket传输网络字节序和本地字节序(干货)

Socket传输网络字节序和本地字节序(干货)

作者: ChineseBoy | 来源:发表于2017-02-16 10:03 被阅读64次

在我们进行Socket自定义协议网络字节传输的时候,我们不能直接使用从网络传输过来的字节数组进行解析,我们首先需要做一次转换。

直接上代码,一个ByteConvert,byte[] , 基本数据类型,String之间的转换:

import java.nio.ByteBuffer;
public class ByteConvert {

    public static byte intToByte(int x) {
        return (byte) x;
    }

    public static int byteToInt(byte b) {
        // Java 总是把 byte 当做有符处理;我们可以通过将其和 0xFF 进行二进制与得到它的无符值
        return b & 0xFF;
    }

    private static ByteBuffer buffer = ByteBuffer.allocate(8);

    // byte 数组与 long 的相互转换
    public static byte[] longToBytes2(long x) {
        buffer.putLong(0, x);
        return buffer.array();
    }

    public static long bytesToLong2(byte[] bytes) {
        buffer.put(bytes, 0, bytes.length);
        buffer.flip();
        return buffer.getLong();
    }
    
    public static char tochar(byte[] x) {
        return (char) ((x[0] << 8) | (x[1] & 0xff));
    }
    // =================================================================================

    public static byte[] longToBytes(long n) {
        byte[] b = new byte[8];
        b[7] = (byte) (n & 0xff);
        b[6] = (byte) (n >> 8 & 0xff);
        b[5] = (byte) (n >> 16 & 0xff);
        b[4] = (byte) (n >> 24 & 0xff);
        b[3] = (byte) (n >> 32 & 0xff);
        b[2] = (byte) (n >> 40 & 0xff);
        b[1] = (byte) (n >> 48 & 0xff);
        b[0] = (byte) (n >> 56 & 0xff);
        return b;
    }

    public static void longToBytes(long n, byte[] array, int offset) {
        array[7 + offset] = (byte) (n & 0xff);
        array[6 + offset] = (byte) (n >> 8 & 0xff);
        array[5 + offset] = (byte) (n >> 16 & 0xff);
        array[4 + offset] = (byte) (n >> 24 & 0xff);
        array[3 + offset] = (byte) (n >> 32 & 0xff);
        array[2 + offset] = (byte) (n >> 40 & 0xff);
        array[1 + offset] = (byte) (n >> 48 & 0xff);
        array[0 + offset] = (byte) (n >> 56 & 0xff);
    }

    public static long bytesToLong(byte[] array) {
        return ((((long) array[0] & 0xff) << 56)
                | (((long) array[1] & 0xff) << 48)
                | (((long) array[2] & 0xff) << 40)
                | (((long) array[3] & 0xff) << 32)
                | (((long) array[4] & 0xff) << 24)
                | (((long) array[5] & 0xff) << 16)
                | (((long) array[6] & 0xff) << 8) | (((long) array[7] & 0xff) << 0));
    }

    public static long bytesToLong(byte[] array, int offset) {
        return ((((long) array[offset + 0] & 0xff) << 56)
                | (((long) array[offset + 1] & 0xff) << 48)
                | (((long) array[offset + 2] & 0xff) << 40)
                | (((long) array[offset + 3] & 0xff) << 32)
                | (((long) array[offset + 4] & 0xff) << 24)
                | (((long) array[offset + 5] & 0xff) << 16)
                | (((long) array[offset + 6] & 0xff) << 8) | (((long) array[offset + 7] & 0xff) << 0));
    }

    public static byte[] intToBytes(int n) {
        byte[] b = new byte[4];
        b[3] = (byte) (n & 0xff);
        b[2] = (byte) (n >> 8 & 0xff);
        b[1] = (byte) (n >> 16 & 0xff);
        b[0] = (byte) (n >> 24 & 0xff);
        return b;
    }

    public static void intToBytes(int n, byte[] array, int offset) {
        array[3 + offset] = (byte) (n & 0xff);
        array[2 + offset] = (byte) (n >> 8 & 0xff);
        array[1 + offset] = (byte) (n >> 16 & 0xff);
        array[offset] = (byte) (n >> 24 & 0xff);
    }

    public static int bytesToInt(byte b[]) {
        return b[3] & 0xff | (b[2] & 0xff) << 8 | (b[1] & 0xff) << 16
                | (b[0] & 0xff) << 24;
    }

    public static int bytesToInt(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 byte[] uintToBytes(long n) {
        byte[] b = new byte[4];
        b[3] = (byte) (n & 0xff);
        b[2] = (byte) (n >> 8 & 0xff);
        b[1] = (byte) (n >> 16 & 0xff);
        b[0] = (byte) (n >> 24 & 0xff);

        return b;
    }

    public static void uintToBytes(long n, byte[] array, int offset) {
        array[3 + offset] = (byte) (n);
        array[2 + offset] = (byte) (n >> 8 & 0xff);
        array[1 + offset] = (byte) (n >> 16 & 0xff);
        array[offset] = (byte) (n >> 24 & 0xff);
    }

    public static long bytesToUint(byte[] array) {
        return ((long) (array[3] & 0xff)) | ((long) (array[2] & 0xff)) << 8
                | ((long) (array[1] & 0xff)) << 16
                | ((long) (array[0] & 0xff)) << 24;
    }

    public static long bytesToUint(byte[] array, int offset) {
        return ((long) (array[offset + 3] & 0xff))
                | ((long) (array[offset + 2] & 0xff)) << 8
                | ((long) (array[offset + 1] & 0xff)) << 16
                | ((long) (array[offset] & 0xff)) << 24;
    }

    public static byte[] shortToBytes(short n) {
        byte[] b = new byte[2];
        b[1] = (byte) (n & 0xff);
        b[0] = (byte) ((n >> 8) & 0xff);
        return b;
    }

    public static void shortToBytes(short n, byte[] array, int offset) {
        array[offset + 1] = (byte) (n & 0xff);
        array[offset] = (byte) ((n >> 8) & 0xff);
    }

    public static short bytesToShort(byte[] b) {
        return (short) (b[1] & 0xff | (b[0] & 0xff) << 8);
    }

    public static short bytesToShort(byte[] b, int offset) {
        return (short) (b[offset + 1] & 0xff | (b[offset] & 0xff) << 8);
    }

    public static byte[] ushortToBytes(int n) {
        byte[] b = new byte[2];
        b[1] = (byte) (n & 0xff);
        b[0] = (byte) ((n >> 8) & 0xff);
        return b;
    }

    public static void ushortToBytes(int n, byte[] array, int offset) {
        array[offset + 1] = (byte) (n & 0xff);
        array[offset] = (byte) ((n >> 8) & 0xff);
    }

    public static int bytesToUshort(byte b[]) {
        return b[1] & 0xff | (b[0] & 0xff) << 8;
    }

    public static int bytesToUshort(byte b[], int offset) {
        return b[offset + 1] & 0xff | (b[offset] & 0xff) << 8;
    }

    public static byte[] ubyteToBytes(int n) {
        byte[] b = new byte[1];
        b[0] = (byte) (n & 0xff);
        return b;
    }

    public static void ubyteToBytes(int n, byte[] array, int offset) {
        array[0] = (byte) (n & 0xff);
    }

    public static int bytesToUbyte(byte[] array) {
        return array[0] & 0xff;
    }

    public static int bytesToUbyte(byte[] array, int offset) {
        return array[offset] & 0xff;
    }
    
    public static String bytesToString(byte[] array){
        return new String(array);
    }
    
    public static String bytesToString(byte[] array,int beginIndex,int len){
        byte[] b = new byte[len];
        for (int i = 0; i < len; i++) {
            b[i] = array[beginIndex+i];
        }
        return new String(b);
    }
    
    public static byte[] bytesToBytes(byte[] array,int beginIndex,int len){
        byte[] b = new byte[len];
        for (int i = 0; i < len; i++) {
            b[i] = array[beginIndex+i];
        }
        return b;
    }
}

相关文章

  • Socket传输网络字节序和本地字节序(干货)

    在我们进行Socket自定义协议网络字节传输的时候,我们不能直接使用从网络传输过来的字节数组进行解析,我们首先需要...

  • Linux的socket API基本操作

    1.socket地址API 主机字节序和网络字节序 字节序分为大端字节序和小端字节序 大端字节序 = 一个整数的高...

  • C++网络编程小心得

    网络当前总结 (乱入)split 字符串 socket 传输字节序问题 由于进行网络传输涉及到大端序小端序问题,基...

  • Linux高性能服务器入门

    第五章 Linux网络编程基础API 主机字节序和网络字节序 <5.1.1>Q:设定主机字节序和网络字节序是为了解...

  • Socket

    Socket socket是套接字,多指传输层网络接口。 Socket和SocketServer是基于套接字的服务...

  • C语言字节序转换API

    主机字节序转网络字节序 网络字节序转主机字节序 将字符串IP地址转换为网络字节序的整型数据 将网络字节序数据转换为...

  • Hadoop序列化

    序列化 把内存中对象转换成字节序列(或其他数据传输协议)以便于存储到磁盘(持久化)和网络传输 反序列化 接收到的字...

  • 大端模式和小端模式

    大端模式 大端模式(Big-Endian)又称大端字节序,由于在网络传输中一般使用的是大端模式,所以也叫网络字节序...

  • Java--对象的序列化和反序列化

    对象序列化的目标: 将对的字节序列对象永久的保存到磁盘中。 允许在网络上直接传输对象,传输对象的字节序列。 对象序...

  • Socket、SocketServer与WebSocket的关系

    from: socket是套接字,多指传输层网络接口。 Socket和SocketServer是基于套接字的服务端...

网友评论

      本文标题:Socket传输网络字节序和本地字节序(干货)

      本文链接:https://www.haomeiwen.com/subject/jsbcwttx.html