Skip to content Skip to sidebar Skip to footer

Android - How To Properly Parse A File Consisting Of Bytes Delimited With Space?

New day, new problems. I'm working on an Android app that gets a stream of raw bytes from a sensor. Each such packet is 19 Bytes long. In order to store that data, I use String.va

Solution 1:

String.getBytes not the same as Bytes.toString. When you convert byte to string you receive number of chars, for example byte value 127 is 3 char ['1','2','7'] with byte codes [49, 50, 55]

byte b = 127;

    System.out.println(b + ""); // 127

String "127".getBytes() returns this array [49, 50, 55]

    System.out.println(Arrays.toString("127".getBytes())); // [49, 50, 55]

To properly read value you need next code:

byte[] bytes = "127".getBytes();

    String value = "";
    for (byte part : bytes) {

        value += (char)part;
    }

    System.out.print(value); // 127

If you want to receive byte value you need parse this string via

byte b = Byte.parseByte(value);

Post a Comment for "Android - How To Properly Parse A File Consisting Of Bytes Delimited With Space?"