将我的32个字符int转换为32个字节的数组,就像在Java

时间:2015-12-07 15:09:39

标签: java arrays byte

我想知道如何将32字符int转换为32字节数组,因为它已被表示。

示例:

我有这个int:

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:对于输入字符串:“0x01”

3 个答案:

答案 0 :(得分:3)

导致问题的原因

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

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

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

请注意,不应在前面添加"0x"。然而,这是一种效率低下的方法。

Ints是32位,而不是32字节

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

数字系统

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

使用字节数组

序列化int(或其他数据结构)

您可以使用this codeint(和其他数据类型)转换为字节数组:

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

答案 1 :(得分:2)

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

每次获取两位数字:

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的类型和值得到适当更新,此代码基本上可用于任何整数类型。

答案 2 :(得分:2)

以下是如何将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]);