public static void writeString(byte[] data, int offset, int length, String string) {

        if(data.length < offset + length) {
            throw new IllegalArgumentException("Not enough space for writing a String at " + offset + " of length " + length);
        }
        for(int i = 0; i < length; i++) {
            if(i >= string.length()) {
                data[offset + i] = 0;
            } else {
                data[offset + i] = (byte) (string.charAt(i) & 0xFF);
            }
        }
    }