0

I got a byte array sent from javascript by ajax looking like this:

"89,50,4e,47,0d,0a,1a,0a,00,00,00,0d,49,48,44,52,00,00,01,98,00,00,00,e4,08,06,00 ..."

I would like to convert this string to a byte array. Pretty similar to this question, but using Java instead: Convert String[] to byte[] array

Edit: This seems to work - but not sure if i am doing it right

    String[] byteData ="89,50,4e,47,0d,0a,1a,0a,00,00,00,0d,49,48,44,52,00,00,01,98,00,00,00,e4,08,06,00";
    byte[] b = new byte[byteData.length];
    for (int i = 0; i < byteData.length; i++) {
        BigInteger bla = BigInteger.valueOf(Integer.parseInt(byteData[i].trim(), 16) - 128);
        b[i] = bla.toByteArray()[0];
    }
3
  • 2
    Have you tried any code yourself? Commented Jun 8, 2017 at 10:34
  • You have String(byte[]), you just need to read the String to decode the bytes one by one to create that byte[], try and come back if you can't. Commented Jun 8, 2017 at 10:37
  • 2
    The general process would be to String::split at the commas, write a convert hexToDec() function, subtract 128 from result, then cast to byte. There is no such thing as a uByte in Java, that is why you would need to subtract 128 from result Commented Jun 8, 2017 at 10:38

4 Answers 4

0

You can try this

String response ="89,50,47,00,00,00,49,48,44,52,00,00,01,98,00,00,00,08";

List<Byte> byteList = Arrays.stream(response.split(","))
   .map(Byte::parseByte)
   .collect(Collectors.toList());

same as previous but with stream xD

but your example throws NumberFormatException on 4e, 0d and so on

Sign up to request clarification or add additional context in comments.

6 Comments

OP wants a byte array, not a List<Byte>
eehm NumberFormatException xD
@JacobG. you can use .toArray(Byte[]::new) instead of .collect(Collectors.toList())
@sofarsoghood are you sure about dat string? Is it 100% valid?
@sofarsoghood have you tried response.getBytes() ? it returns byte[] from string
|
0

here is a simple solution with JAVA :

        String bytes="89,50,4e,47,0d,0a,1a,0a,00,00,00,0d,49,48,44,52,00,00,01,98,00,00,00,e4,08";
        String []tab=bytes.split(",");
        Integer []b = new Integer[tab.length];
        for(int i = 0 ; i < tab.length ; i++){
             if(tab[i].matches("[-+]?\\d*\\.?\\d+"))
                 b[i]=Integer.parseInt(tab[i]);
             else
                 b[i]=Integer.parseInt(tab[i],16);


        }

6 Comments

java.lang.NumberFormatException: For input string: "4e" at b[i]=Byte.parseByte(tab[i]);
i edited the answer , this will solve the problem : b[i]=Byte.parseByte(tab[i],16);
Unfortunatelly this cause problem: Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"89" Radix:16
hmm java.lang.NumberFormatException: Value out of range. Value:"89" Radix:16 at b[i]=Byte.parseByte(tab[i],16); :/
so we have to test if the string is numeric or not , if it's not u parse it as hex , try it now :) if(tab[i].matches("[-+]?\\d*\\.?\\d+")) b[i]=Byte.parseByte(tab[i]); else b[i]=Byte.parseByte(tab[i],16);
|
0

Try below code (output is result of conversion):

    String input = "89,50,4e,47,0d,0a,1a,0a,00,00,00,0d,49,48,44,52,00,00,01,98,00,00,00,e4,08";

    String[] hex = input.split(",");
    char[] chars = new char[hex.length];
    for (int i = 0; i < hex.length; i++) {
        chars[i] = (char) Integer.parseInt(hex[i], 16);
    }
    String output = new String(chars);

Comments

0

EDIT: Actually parseByte doesn't seems to be a safe choice for your case because of this, so I took inspiration from this answer to change it.

You can do:

String bytesAsStringWithCommas = "89,50,4e,47,0d,0a,1a,0a,00,00,00,0d,49,48,44,52,00,00,01,98,00,00,00,e4,08,06,00 ...";
String[] bytesStr = bytesAsStringWithCommas.split(",");
byte[] byteArray = new byte[bytesStr.length];
for (int j = 0; j < bytesStr.length; j++) {
    byteArray[j] = (byte) ((Character.digit(bytesStr[j].charAt(0), 16) << 4) + Character.digit(bytesStr[j].charAt(1), 16));
}

You should probably change your server code or client code to send data directly instead of using convoluted solutions like this one. Or you can also use Base64 encoding, which is going to be already better than your current solution.

2 Comments

parseByte requires 2nd argument 16.
You are perfectly right, thx. But I found that parseByte is not a very good choice actually (see edit).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.