Java examples for java.lang:short
Print an octet string.
//package com.java2s; public class Main { public static void main(String[] argv) throws Exception { byte[] bytes = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 }; System.out.println(toOctetString(bytes)); }//w ww .j a va 2 s .c om /** * Print an octet string. This can be used to encode bytes that may * represent an unknown entity. This will be in the form of: * * 00-11-22-33 ... * */ public static String toOctetString(byte[] bytes) { StringBuffer sb = new StringBuffer(4 * bytes.length); for (int i = 0; i < bytes.length; i++) { sb.append(unsignedByteToInt(bytes[i])); if (i + 1 < bytes.length) { sb.append("-"); } } return sb.toString(); } /** * Convert an unsigned byte to an int */ public static int unsignedByteToInt(byte b) { return (int) b & 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(); } }