将我的 32 字符 int 转换为 32 字节数组,如 Java

turn my 32 character int into a 32 byte array as is in Java

我想知道如何将我的 32 字符 int 转换为表示的 32 字节数组。

示例:

我有这个整数:

int test = 123456789;

我想把它变成这样:

byte[] Write_Page_Four = new byte[] {  

                                (byte) 0x00, (byte) 0x00,
                                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                                (byte) 0x00, (byte) 0x01, (byte) 0x23, (byte) 0x45,
                                (byte) 0x67, (byte) 0x89};

目前,我正在考虑将我的 int 拆分为 2,然后手动将它们分配给字节数组,但我在这样做时遇到了一些麻烦,而且我认为这不是解决我的问题的最佳做法。

这就是我的 ATM,它正在返回错误,但仍在进行中,我可以使用一些建议:

String test2 = "01";
String test1 = "0x"+test2; 
byte test =  Byte.valueOf(test1);
System.out.println("teeeeest-----"+test);

byte[] Write_Page_Four = new byte[] {(byte) test};

而这个返回错误:

java.lang.NumberFormatException: For input string: "0x01"

导致问题的原因

Byte.valueOf 不像 Java 编译器那样解析数据:它期望输入为十进制数。

但是,您可以使用具有任意基数的 Byte.valueOf(String,int)。在这种情况下,您可以使用以下方法解决它:

byte test =  Byte.valueOf(test2,16); //using test2, not test1

介意不要在前面加"0x"。然而,这是一种低效的方法。

整数是 32 位,而不是 32 字节

第二个问题是您声明可以将 12345678901234567890123456789011 之类的数字存储到 int 中。你不能。一个 int 有 32 。这意味着它的表示仅限于或多或少 2.1B。所以我认为您的意思是将 12345678901234567890123456789011 存储在 String?

数系

请注意,除非您使用 二进制编码的小数 ,否则数字 12345678901234567890123456789011 不会在内部表示为 (byte) 0x12, (byte) 0x34,...。这是因为计算机使用二进制数字系统(因此使用十六进制表示对字节进行分组),而人类使用十进制表示。例如 123456789 将表示为 0x07,0x5B,0xCD 0x15.

使用字节数组序列化一个 int(或其他数据结构)

您可以使用 this code:

int(和其他数据类型)转换为字节数组
ByteBuffer b = ByteBuffer.allocate(4);
b.putInt(test);
byte[] result = b.array(); //result will be 4 bytes,
//since you can represent any int with four bytes.

或者,如果您想像这样表示 int,可以使用以下方法:

int t = test;
byte[] dat = new byte[5];//at most 5 bytes needed
for(int j = 4; test != 0; j--) {
    int rm = t%100;
    dat[j] = (byte) (rm%10+((rm/10)<<8));
    t /= 100;
}
//result is dat

我建议不要处理数字的文本表示,而是简单地计算单个数字:

每次取两位数:

int inp = 1234...;

for(int lowerBound = 1 ; lowerBound < Integer.MAX_VALUE ; lowerBound *= 100)
    //twoDigit contains two digits of the input-number
    int twoDigit = (inp /lowerBound) % 100;

将这两个数字转换成一个字节:

byte transform(int i){
    if(i == 0)
        return 0x00;

    int lsd = (i % 10); //least significant digit of the input (decimal)
    int msd = (i / 10); //most significant digit

    //merge lsd and msd into a single number, where the lower 4 bits are reserved for
    //lsd and the higher 4 bits for msd
    return lsd | (msd << 4);
}

完整的代码如下所示:

import java.util.Arrays;

public class test
{
    private static final int BYTES = 4;

    public static void main(String[] args){
        int v = 12345678;

        int at_arr = BYTES - 1;
        byte[] result = new byte[BYTES];//int is 32-bit/4 byte long

        for(int lowerBound = 1 ; lowerBound < Integer.MAX_VALUE && at_arr > -1; lowerBound *= 100, at_arr--)
            result[at_arr] = transformDigits((v / lowerBound) % 100);

        for(byte b : result)
            System.out.print(" 0x" + Integer.toString(b , 16) + ",");
        System.out.println();
    }

    static byte transformDigits(int i){
        if(i == 0)
            return 0x00;

        int lsd = (i % 10); //least significant digit of the input (decimal)
        int msd = (i / 10); //most significant digit

        //merge lsd and msd into a single number, where the lower 4 bits are reserved for
        //lsd and the higher 4 bits for msd
        return (byte) (lsd | (msd << 4));
    }
}

如果适当更新 BYTES 的类型和值,此代码基本上可用于任何整数类型。

以下是将 int 转换为 byte[] 的方法:

int test = 123456789;

byte[] bytes = new byte[4];
bytes[0] = (byte)(test >> 24);
bytes[1] = (byte)(test >> 16);
bytes[2] = (byte)(test >> 8);
bytes[3] = (byte)test;

System.out.printf("%02x %02x %02x %02x%n", bytes[0], bytes[1], bytes[2], bytes[3]);

输出

07 5b cd 15

如果需要,您也可以将其内联:

int test = 123456789;
byte[] bytes = new byte[] { (byte)(test >> 24),
                            (byte)(test >> 16),
                            (byte)(test >> 8),
                            (byte)test };
System.out.printf("%02x %02x %02x %02x%n", bytes[0], bytes[1], bytes[2], bytes[3]);