Java examples for File Path IO:Byte Array
Converts an integer to a byte array with a length of 4.
//package com.java2s; public class Main { public static void main(String[] argv) throws Exception { int a = 2; byte[] buf = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 }; integerToByteArray(a, buf);/*from w ww. j av a2 s.c om*/ } /** * Converts an integer to a byte array with a length of 4. * @param a - The integer to convert. * @param buf - The byte array to store the integer in. This must be a length of at least 4. */ public static void integerToByteArray(int a, byte[] buf) { buf[0] = (byte) ((a >> 24) & 0xFF); buf[1] = (byte) ((a >> 16) & 0xFF); buf[2] = (byte) ((a >> 8) & 0xFF); buf[3] = (byte) (a & 0xFF); } }