Java - short、int、long 与 byte 数组互转

示例

long 类型的变量与 byte 数组互转:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class ByteUtilsTests {

    public static void main(String[] args) throws IOException {
        long currentTimeMillis = System.currentTimeMillis();
        System.out.println(currentTimeMillis);
        
        // long 类型变量转 byte 数组
        ByteArrayOutputStream baos = new ByteArrayOutputStream(Long.BYTES);
        new DataOutputStream(baos).writeLong(currentTimeMillis);
      
        byte[] bytes = baos.toByteArray();
        
        // byte 数组转 long 类型变量
        long number = new DataInputStream(new ByteArrayInputStream(bytes)).readLong();
        
        System.out.println(number);
    }
}

输出:

1610638668277
1610638668277

工具类

将其封装成工具类:

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

public class ByteUtils {
    
    public static byte[] toByteArray(short number) {
        byte[] bytes = new byte[Short.BYTES];
        
        bytes[0] = (byte) (number >> 8);
        bytes[1] = (byte) number;
        
        return bytes;
    }
    
    public static byte[] toByteArray(int number) {
        byte length = Integer.BYTES;
        byte[] bytes = new byte[length];

        for (byte i = 0; i < length; i++) {
            bytes[length - 1 - i] = (byte) number;
            number >>= 8;
        }
        
        return bytes;
    }
    
    public static byte[] toByteArray(long number) {
        byte length = Long.BYTES;
        byte[] bytes = new byte[length];

        for (byte i = 0; i < length; i++) {
            bytes[length - 1 - i] = (byte) number;
            number >>= 8;
        }
        
        return bytes;
        
//        ByteArrayOutputStream baos = new ByteArrayOutputStream(length);
//        new DataOutputStream(baos).writeLong(number);
//        return baos.toByteArray();
    }
    
    public static short toShort(byte[] bytes) throws IOException {
        return new DataInputStream(new ByteArrayInputStream(bytes)).readShort();
    }
    
    public static int toInteger(byte[] bytes) throws IOException {
        return new DataInputStream(new ByteArrayInputStream(bytes)).readInt();
    }
    
    public static long toLong(byte[] bytes) throws IOException {
        return new DataInputStream(new ByteArrayInputStream(bytes)).readLong();
    }
}

测试:

import java.io.IOException;

import com.mk.util.ByteUtils;

public class ByteUtilsTests {

    public static void main(String[] args) throws IOException {
        byte[] bytes = null;
        
        bytes = ByteUtils.toByteArray((short) 0xF123); // 负数
        System.out.println((short) 0xF123);
        System.out.println(ByteUtils.toShort(bytes));
        
        bytes = ByteUtils.toByteArray((short) 0x1234); // 正数
        System.out.println((short) 0x1234);
        System.out.println(ByteUtils.toShort(bytes));
        
        bytes = ByteUtils.toByteArray(0xF1234567);
        System.out.println(0xF1234567);
        System.out.println(ByteUtils.toInteger(bytes));
        
        bytes = ByteUtils.toByteArray(0x12345678);
        System.out.println(0x12345678);
        System.out.println(ByteUtils.toInteger(bytes));
        
        bytes = ByteUtils.toByteArray(0xF123456789ABCDEFL);
        System.out.println(0xF123456789ABCDEFL);
        System.out.println(ByteUtils.toLong(bytes));
        
        bytes = ByteUtils.toByteArray(0x123456789ABCDEF0L);
        System.out.println(0x123456789ABCDEF0L);
        System.out.println(ByteUtils.toLong(bytes));
    }
}

输出:

-3805
-3805
4660
4660
-249346713
-249346713
305419896
305419896
-1070935975390360081
-1070935975390360081
1311768467463790320
1311768467463790320
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值