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];
    }
sofarsoghood
  • 243
  • 2
  • 16
  • 2
    Have you tried any code yourself? – ItamarG3 Jun 08 '17 at 10:34
  • You have [`String(byte[]`)](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#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. – AxelH Jun 08 '17 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 – CraigR8806 Jun 08 '17 at 10:38

4 Answers4

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);


        }
Mohamed Ali RACHID
  • 3,245
  • 11
  • 22
  • java.lang.NumberFormatException: For input string: "4e" at `b[i]=Byte.parseByte(tab[i]);` – sofarsoghood Jun 08 '17 at 11:01
  • i edited the answer , this will solve the problem : `b[i]=Byte.parseByte(tab[i],16);` – Mohamed Ali RACHID Jun 08 '17 at 11:16
  • Unfortunatelly this cause problem: `Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"89" Radix:16` – tmucha Jun 08 '17 at 11:23
  • hmm java.lang.NumberFormatException: Value out of range. Value:"89" Radix:16 at `b[i]=Byte.parseByte(tab[i],16);` :/ – sofarsoghood Jun 08 '17 at 11:24
  • 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);` – Mohamed Ali RACHID Jun 08 '17 at 11:29
  • because e4 is 228 in Decimal , so u have to store in an Integer not in Byte , i updated the answer , i tested it , it works now :) – Mohamed Ali RACHID Jun 08 '17 at 11:54
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.

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

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);
tmucha
  • 689
  • 1
  • 4
  • 19