zoukankan      html  css  js  c++  java
  • Java中数值类型与字节数组之间的转换大法(精简)

    /**
    * 整型转字节数组
    *
    * @param data 待转换数值
    * @param bytes 转换后的数组
    * @param beginIndex 数组起始下标
    * @return
    */
    public static int int2Bytes(int data, byte[] bytes, int beginIndex) {
    if (null == bytes || beginIndex < 0 || bytes.length < beginIndex + 4) {
    return -1;
    }

    try {
    bytes[beginIndex++] = (byte) (data & 0xff);
    bytes[beginIndex++] = (byte) (data >> 8 & 0xff);
    bytes[beginIndex++] = (byte) (data >> 16 & 0xff);
    bytes[beginIndex++] = (byte) (data >> 24 & 0xff);
    } catch (RuntimeException e) {
    throw e;
    }

    return beginIndex;
    }

    /**
    * 长整型转字节数组
    *
    * @param data 待转换数值
    * @param bytes 转换后的数组
    * @param beginIndex 数组起始下标
    * @return
    */
    public static int long2Bytes(long data, byte[] bytes, int beginIndex) {
    if (null == bytes || beginIndex < 0 || bytes.length < beginIndex + 8) {
    return -1;
    }

    try {
    bytes[beginIndex++] = (byte) (data & 0xff);
    bytes[beginIndex++] = (byte) (data >> 8 & 0xff);
    bytes[beginIndex++] = (byte) (data >> 16 & 0xff);
    bytes[beginIndex++] = (byte) (data >> 24 & 0xff);
    bytes[beginIndex++] = (byte) (data >> 32 & 0xff);
    bytes[beginIndex++] = (byte) (data >> 40 & 0xff);
    bytes[beginIndex++] = (byte) (data >> 48 & 0xff);
    bytes[beginIndex++] = (byte) (data >> 56 & 0xff);
    } catch (RuntimeException e) {
    throw e;
    }

    return beginIndex;
    }

    /**
    * 字节数组转整型
    *
    * @param bytes 待转换数组
    * @param beginIndex 数组起始下标
    * @return
    */
    public static int bytes2Int(byte[] bytes, int beginIndex) {
    if (null == bytes || beginIndex < 0 || bytes.length < beginIndex + 4) {
    return -1;
    }

    int result = 0;
    try {
    int tmpVal = 0;
    for (int i = 0; i < 4; i++) {
    tmpVal = (bytes[i + beginIndex] << (i * 8));
    switch (i) {
    case 0:
    tmpVal = tmpVal & 0x000000FF;
    break;
    case 1:
    tmpVal = tmpVal & 0x0000FF00;
    break;
    case 2:
    tmpVal = tmpVal & 0x00FF0000;
    break;
    case 3:
    tmpVal = tmpVal & 0xFF000000;
    break;
    }

    result |= tmpVal;
    }
    } catch (RuntimeException e) {
    throw e;
    }

    return result;
    }

    /**
    * 字节数组转长整型
    *
    * @param bytes 待转换数组
    * @param beginIndex 数组起始下标
    * @return
    */
    public static long bytes2Long(byte[] bytes, int beginIndex) {
    if (null == bytes || beginIndex < 0 || bytes.length < beginIndex + 8) {
    return -1L;
    }

    long result = 0L;
    try {
    int shift = 0;
    for (int i = 0; i < 8; i++) {
    shift = i << 3;
    result |= ((long) 0xff << shift) & ((long) bytes[i + beginIndex] << shift);
    }
    } catch (RuntimeException e) {
    throw e;
    }

    return result;
    }
  • 相关阅读:
    python多线程学习一
    https学习笔记三----OpenSSL生成root CA及签发证书
    https学习笔记二----基础密码学知识和python pycrypto库的介绍使用
    HTTPS学习笔记一----HTTPS的基础理论知识
    一只小鹅的2017
    python 全局变量的import机制
    一起来学设计模式-----工厂模式的实践
    一起来学设计模式-----创建型模式之抽象工厂
    一起来学设计模式-----创建型模式之工厂方法
    一起来学设计模式-----创建型模式之简单工厂
  • 原文地址:https://www.cnblogs.com/chenyixun/p/14052420.html
Copyright © 2011-2022 走看看