ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-01-15 00:00:20  阅读:206  来源: 互联网

标签:short Java bytes System 互转 new byte ByteUtils out


示例

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) throws IOException {
        byte[] bytes = new byte[Short.BYTES];
        
        bytes[0] = (byte) (number >> 8);
        bytes[1] = (byte) number;
        
        return bytes;
    }
    
    public static byte[] toByteArray(int number) throws IOException {
        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) throws IOException {
        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

标签:short,Java,bytes,System,互转,new,byte,ByteUtils,out
来源: https://blog.csdn.net/qq_29761395/article/details/112645304

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有