zoukankan      html  css  js  c++  java
  • 【转】java byte转long、double、float、int、short,或者long、double、float、int、short转byte

    原文网址:http://www.xuebuyuan.com/988752.html

    java byte与其他数据类型的转换主要用于二进制数据的编码和解码,主要用于网络传输,读写二进制文件,java和c++服务器之间的数据通信等等

    以下是总结的源码

    /**
    * BYTE转INT
    *
    * @param b
    * @return
    */
    protected int byteArrayToInt(byte[] b) {
    return (b[0] << 24) + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8)
    + (b[3] & 0xFF);
    }
    /**
    * BYTE转SHORT
    *
    * @param b
    * @return
    */
    protected int byteArrayToShort(byte[] b) {
    return (b[0] << 8) + (b[1] & 0xFF);
    }

    /**
    * SHORT转BYTE数据
    *
    * @param s
    * @return
    */
    protected byte[] shortToByteArray(short s) {
    byte[] shortBuf = new byte[2];
    for (int i = 0; i < 2; i++) {
    int offset = (shortBuf.length - 1 - i) * 8;
    shortBuf[i] = (byte) ((s >>> offset) & 0xff);
    }
    return shortBuf;
    }

    /**
    * INT数据转BYTE数据
    *
    * @param i
    * @return
    */
    protected byte[] intToByteArray(int i) {
    byte[] result = new byte[4];
    result[0] = (byte) ((i >> 24) & 0xFF);
    result[1] = (byte) ((i >> 16) & 0xFF);
    result[2] = (byte) ((i >> 8) & 0xFF);
    result[3] = (byte) (i & 0xFF);
    return result;
    }

    /**
    * 转换long型为byte数组
    *
    * @param bb
    * @param x
    * @param index
    */
    public byte[] longToByteArray(long x, int index) {
    byte[] bb = new byte[8];
    bb[index + 7] = (byte) (x >> 56);
    bb[index + 6] = (byte) (x >> 48);
    bb[index + 5] = (byte) (x >> 40);
    bb[index + 4] = (byte) (x >> 32);
    bb[index + 3] = (byte) (x >> 24);
    bb[index + 2] = (byte) (x >> 16);
    bb[index + 1] = (byte) (x >> 8);
    bb[index + 0] = (byte) (x >> 0);
    return bb;
    }

    /**
    * 通过byte数组取到long
    *
    * @param bb
    * @param index
    * @return
    */
    public long byteArrayToLong(byte[] bb, int index) {
    return ((((long) bb[index + 7] & 0xff) << 56)
    | (((long) bb[index + 6] & 0xff) << 48)
    | (((long) bb[index + 5] & 0xff) << 40)
    | (((long) bb[index + 4] & 0xff) << 32)
    | (((long) bb[index + 3] & 0xff) << 24)
    | (((long) bb[index + 2] & 0xff) << 16)
    | (((long) bb[index + 1] & 0xff) << 8) | (((long) bb[index + 0] & 0xff) << 0));
    }

    /**
    * float转换byte
    *
    * @param bb
    * @param x
    * @param index
    */
    public static byte[] floatTobyteArray(float v) {
    ByteBuffer bb = ByteBuffer.allocate(4);
    byte[] ret = new byte[4];
    FloatBuffer fb = bb.asFloatBuffer();
    fb.put(v);
    bb.get(ret);
    return ret;
    }

    /**
    * 通过byte数组取得float
    *
    * @param bb
    * @param index
    * @return
    */
    public static float byteArrayToFloat(byte[] v) {
    ByteBuffer bb = ByteBuffer.wrap(v);
    FloatBuffer fb = bb.asFloatBuffer();
    return fb.get();
    }

    /**
    * double转换byte
    *
    * @param bb
    * @param x
    * @param index
    */
    public byte[] doubleToByteArray(double x) {
    ByteBuffer bb = ByteBuffer.allocate(8);
    byte[] ret = new byte[8];
    DoubleBuffer fb = bb.asDoubleBuffer();
    fb.put(x);
    bb.get(ret);
    return ret;
    }

    /**
    * 通过byte数组取得float
    *
    * @param bb
    * @param index
    * @return
    */
    public double byteArrayToDouble(byte[] b) {
    ByteBuffer bb = ByteBuffer.wrap(b);
    DoubleBuffer fb = bb.asDoubleBuffer();
    return fb.get();
    }

  • 相关阅读:
    URL编码与解码
    什么通用数据交换格式更好
    JSON(JavaScript Object Notation)
    二维码与json都是数据交换格式
    数据的存在形式
    NSData、数据结构与数据转换
    物理结构与逻辑结构
    NSKeyedArchiver : NSCoder
    The Role of View Controllers
    Content-Type与MIME
  • 原文地址:https://www.cnblogs.com/wi100sh/p/4834553.html
Copyright © 2011-2022 走看看