Java –将byte []转换为int,反之亦然

在Java中,我们可以使用ByteBufferint转换为byte[] ,反之亦然。

intbyte[]

int num = 1;
  // int need 4 bytes, default ByteOrder.BIG_ENDIAN
  byte[] result =  ByteBuffer.allocate(4).putInt(number).array();

byte[]int

byte[] byteArray = new byte[] {00, 00, 00, 01};
  int num = ByteBuffer.wrap(bytes).getInt();

1. int到byte []

这个Java示例将一个int转换为一个字节数组,并以十六进制格式打印它。

IntToByteArrayExample.java
package com.mkyong.nio;

import java.nio.ByteBuffer;

public class IntToByteArrayExample {

    public static void main(String[] args) {

        int num = 1;
        byte[] result = convertIntToByteArray(num);

        System.out.println("Input            : " + num);
        System.out.println("Byte Array (Hex) : " + convertBytesToHex(result));

    }

    // method 1, int need 4 bytes, default ByteOrder.BIG_ENDIAN
    public static byte[] convertIntToByteArray(int value) {
        return  ByteBuffer.allocate(4).putInt(value).array();
    }

    // method 2, bitwise right shift
    public static byte[] convertIntToByteArray2(int value) {
        return new byte[] {
                (byte)(value >> 24),
                (byte)(value >> 16),
                (byte)(value >> 8),
                (byte)value };
    }

    public static String convertBytesToHex(byte[] bytes) {
        StringBuilder result = new StringBuilder();
        for (byte temp : bytes) {
            result.append(String.format("%02x", temp));
        }
        return result.toString();
    }

}

输出量

Terminal
Input            : 1
Byte Array (Hex) : 00000001

2. byte []转换为int

ByteArrayToIntExample.java
package com.mkyong.nio;

import java.nio.ByteBuffer;

public class ByteArrayToIntExample {

    public static void main(String[] args) {

        // byte = -128 to 127
        byte[] byteArray = new byte[] {00, 00, 00, 01};

        int result = convertByteArrayToInt2(byteArray);

        System.out.println("Byte Array (Hex) : " + convertBytesToHex(byteArray));
        System.out.println("Result           : " + result);

    }

    // method 1
    public static int convertByteArrayToInt(byte[] bytes) {
        return ByteBuffer.wrap(bytes).getInt();
    }

    // method 2, bitwise again, 0xff for sign extension
    public static int convertByteArrayToInt2(byte[] bytes) {
        return ((bytes[0] & 0xFF) << 24) |
                ((bytes[1] & 0xFF) << 16) |
                ((bytes[2] & 0xFF) << 8) |
                ((bytes[3] & 0xFF) << 0);
    }

    public static String convertBytesToHex(byte[] bytes) {
        StringBuilder result = new StringBuilder();
        for (byte temp : bytes) {
            result.append(String.format("%02x", temp));
        }
        return result.toString();
    }

}

输出量

Terminal
Byte Array (Hex) : 00000001
Result           : 1

参考文献

翻译自: https://mkyong.com/java/java-convert-byte-to-int-and-vice-versa/

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值