如何将十六进制字符串直接转换为字节数组?

时间:2022-10-30 15:55:00

I want to convert a string into hex, then into a byte array. Here is my code so far:

我想将字符串转换为十六进制,然后转换为字节数组。这是我到目前为止的代码:

public static void findKey(){

    Cipher cipher;
    SecretKeySpec key;
    byte [] keyBytes;
    byte [] pt;
    byte [] ct;

    String plaintext = "Plaintxt";
    ct = new byte [] {(byte)0x4A, (byte)0xC4, (byte)0x55, (byte)0x3D, (byte)0xB3, (byte)0x37, (byte)0xCA, (byte)0xB3};
    String ptHex = asciiToHex(plaintext);
    System.out.println(ptHex);
    pt = ptHex.getBytes();
    printByteArray(pt);
}

My method to convert to hex works fine, but when I use getBytes, it obviously turns it to 16 bytes which is not what I want. That was just an attempt. Here is the output from just printing my string to make sure it worked and then printing the byte array which is incorrect:

我转换为十六进制的方法工作正常,但是当我使用getBytes时,它显然将它变为16字节,这不是我想要的。那只是一次尝试。这是打印我的字符串的输出,以确保它工作,然后打印不正确的字节数组:

506c61696e747874
[  35  30  36  63  36  31  36  39  36  65  37  34  37  38  37  34  ]
-------------Key  Found-------------

I want to take the 50, the 6c, the 61 etc., and put it into a byte array like I did for ct like 0x50, 0x6c, and so on.

我想取50,6c,61等,并把它放入一个字节数组,就像我为ct做的那样,如0x50,0x6c,依此类推。

Is this even possible?

这有可能吗?

4 个解决方案

#1


2  

try

byte[] getBytes(String s) {
    byte[] a = new byte[s.length() / 2];
    for (int i = 0; i < a.length; i++) {
        a[i] = Byte.parseByte(s.substring(i * 2, i * 2 + 2), 16);
    }
    return a;
}

#2


2  

it might help you

它可能会帮助你

Byte.parseByte(string, 16);

check API

also check this discussion

还请查看此讨论

#3


0  

Use this one-liner, which employs the JDK's API:

使用这个使用JDK API的单行程序:

byte[] array = ByteBuffer.allocate(8).putLong(Long.parseLong(chars, 16)).array();

Here's a test:

这是一个测试:

import java.nio.ByteBuffer;

public static void main(String[] args) throws Exception {
    String chars= "506c61696e747874";
    byte[] array = ByteBuffer.allocate(8).putLong(Long.parseLong(chars, 16)).array();
    System.out.println(Arrays.toString(array));
}

Output:

[80, 108, 97, 105, 110, 116, 120, 116]

Of course the output here is in decimal, but the values are correct.

当然这里的输出是十进制的,但值是正确的。

#4


0  

Simple java method for converting hex to byte array:

将十六进制转换为字节数组的简单java方法:

byte[] byteArray= new BigInteger(hexvalue, 2).toByteArray();

#1


2  

try

byte[] getBytes(String s) {
    byte[] a = new byte[s.length() / 2];
    for (int i = 0; i < a.length; i++) {
        a[i] = Byte.parseByte(s.substring(i * 2, i * 2 + 2), 16);
    }
    return a;
}

#2


2  

it might help you

它可能会帮助你

Byte.parseByte(string, 16);

check API

also check this discussion

还请查看此讨论

#3


0  

Use this one-liner, which employs the JDK's API:

使用这个使用JDK API的单行程序:

byte[] array = ByteBuffer.allocate(8).putLong(Long.parseLong(chars, 16)).array();

Here's a test:

这是一个测试:

import java.nio.ByteBuffer;

public static void main(String[] args) throws Exception {
    String chars= "506c61696e747874";
    byte[] array = ByteBuffer.allocate(8).putLong(Long.parseLong(chars, 16)).array();
    System.out.println(Arrays.toString(array));
}

Output:

[80, 108, 97, 105, 110, 116, 120, 116]

Of course the output here is in decimal, but the values are correct.

当然这里的输出是十进制的,但值是正确的。

#4


0  

Simple java method for converting hex to byte array:

将十六进制转换为字节数组的简单java方法:

byte[] byteArray= new BigInteger(hexvalue, 2).toByteArray();