Soros
Soros

Reputation: 3

How to convert "0xCA" String to byte?

I have a big file with more than 100000 bytes that look like this "0xCA,0xFE,0xBA,0xBE,0x0,0x0,0x0,0x34,0x0,0xBB,0x1,0x0,0x35,0x6D,0x65,0x2F, 0x6D" I would like to retrieve the content of the file and store each byte in a byte array as a byte, my jar retrieves them as Strings but then impossible to transform them into functional byte how can I do?

    public static byte[] genClasses() throws IOException {
    InputStream stream = LonelyMod.class.getResourceAsStream("/o.txt");
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
    ArrayList<Byte> bytes = new ArrayList<>();
    String line;
    while((line = reader.readLine()) != null) {
        ArrayList<Byte> tempBytes = new ArrayList<>();
        for(String s : line.split(",")) {
            tempBytes.add(/* here im supposed to add the s String as a byte*/);
        }
        bytes.addAll(tempBytes);
    }
    byte[] bytes1 = new byte[bytes.size()];
    int i = 0;
    for(byte b : bytes) {
        bytes1[i] = b;
        i++;
    }
    return bytes1;
}

thanks for your help

Upvotes: 0

Views: 662

Answers (3)

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78589

Taking inspiration from @WJS' answer, here's another way, in this case using Integer.decode which can accept the integers in the 0x format you already have them:

String s ="0xCA,0xFE,0xBA,0xBE,0x0,0x0,0x0,0x34,0x0,0xBB,0x1,0x0,0x35,0x6D,0x65,0x2F,0x6D";

Arrays.stream(s.split(","))
        .map(Integer::decode)
        .map(Integer::byteValue)
        .forEach(b -> System.out.printf(" %d", b));

Upvotes: 1

xXMavhawkXx
xXMavhawkXx

Reputation: 37

I see a problem right off the bat. 0xCA > byte.MAX_VALUE, thus giving it a value of -54 instead of the int value of 202. If this is what you want, then here is the code for it. If not, then just remove the byte cast and set x to an int instead.

    byte x = (byte)0;
    if(myByte.length() == "0xCA".length())
    x = (byte)((myByte.substring(0, 1).equals("0") ? 1 : -1) * (myByteVal(myByte.substring(2,3)) * 16 + myByteVal(myByte.substring(3,4))));
    else if(myByte.length() == "0x0".length())
    x = (byte)((myByte.substring(0, 1).equals("0") ? 1 : -1) * (myByteVal(myByte.substring(2,3))));

And the method I created for getting the value is here:

public static int myByteVal(String x) {
        x = x.toLowerCase();
        return x.equals("0") ? 0 : x.equals("1") ? 1 : x.equals("2") ? 2 : x.equals("3") ? 3 : x.equals("4") ? 4 : x.equals("5") ? 5 : x.equals("6") ? 6 : x.equals("7") ? 7 : x.equals("8") ? 8 : x.equals("9") ? 9 : x.equals("a") ? 10 : x.equals("b") ? 11 : x.equals("c") ? 12 : x.equals("d") ? 13 : x.equals("e") ? 14 : 15;
    }

Upvotes: 0

WJS
WJS

Reputation: 40034

Given the following string.

String s ="0xCA,0xFE,0xBA,0xBE,0x0,0x0,0x0,0x34,0x0,0xBB,0x1,0x0,0x35,0x6D,0x65,0x2F,0x6D";

You can do it as follows:

// remove the hex prefix and split on ','
String[] tokens = s.replace("0x","").split(",");

// allocate a byte array to hold the results
byte[] bytes = new byte[tokens.length];

//now parse to an int and assign to a byte.  Only the low order
// 8 bits will be assigned.
int i = 0;
for (String str : tokens) {
    bytes[i++] = (byte) Integer.parseInt(str,16);
}

for (byte b : bytes)
    System.out.print(b + " ");
}

Prints

-54 -2 -70 -66 0 0 0 52 0 -69 1 0 53 109 101 47 109 

Since some are greater 127 they will be printed as signed values.

Upvotes: 2

Related Questions