Skip to content

BytesUtils

Julien Millau edited this page Jul 7, 2014 · 8 revisions

This class provided static methods to convert byte array to String, String to byte array, int to byte array, byte array to binary representation.

Convert byte array to String

    byte[] array = new byte[]{0x20,0x30,0x40};

    System.out.println(BytesUtils.bytesToString(array));
    // display "20 30 40"

    System.out.println(BytesUtils.bytesToStringNoSpace(array));
    // display "203040"

Convert String to byte array Each byte must have two hex digits.

    String bytes = "04 12 34";
    byte[] array = BytesUtils.fromString(bytes);

Convert Int/Integer to byte array Return an array of 4 bytes

    int val = 1;
    byte[] array = BytesUtils.byteArrayToInt(val); // return [0x00,0x00,0x00,0x01]
Clone this wiki locally