Java examples for java.lang:byte Array Convert
Converts a 8-bit unsigned int to a byte array
//package com.java2s; public class Main { public static void main(String[] argv) throws Exception { int value = 2; System.out.println(java.util.Arrays.toString(fromUInt8(value))); }/* w ww . j ava2 s. c o m*/ /** * Converts a 8-bit unsigned int to a byte array */ public static byte[] fromUInt8(int value) { if (value < 0) { throw new IllegalArgumentException( "Must be less greater than 0"); } return new byte[] { (byte) (value & 0xFF) }; } /** * Print a byte array as a String */ public static String toString(byte[] bytes) { StringBuilder sb = new StringBuilder(4 * bytes.length); sb.append("["); for (int i = 0; i < bytes.length; i++) { sb.append(unsignedByteToInt(bytes[i])); if (i + 1 < bytes.length) { sb.append(","); } } sb.append("]"); return sb.toString(); } /** * Convert an unsigned byte to an int */ public static int unsignedByteToInt(byte b) { return (int) b & 0xFF; } }