Java tutorial
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.IOException; public class Main { public static byte[] getResponeData(int index, int resultCode, String response) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(index); outputStream.write(0); outputStream.write(resultCode); try { outputStream.write(response.getBytes()); } catch (IOException e) { e.printStackTrace(); } return outputStream.toByteArray(); } public static byte[] getResponeData(int index, int resultCode, byte[] response) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(index); outputStream.write(1); outputStream.write(resultCode); try { outputStream.write(response); } catch (IOException e) { e.printStackTrace(); } return outputStream.toByteArray(); } }