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

猜你喜欢

转载自blog.csdn.net/qq_29761395/article/details/112645304