Here you can find the source of bytes2HexSplit(byte[] in, int wordlength)
Parameter | Description |
---|---|
in | input array of bytes to convert |
wordlength | the length of hex words to print otu. |
public static String bytes2HexSplit(byte[] in, int wordlength)
//package com.java2s; //License from project: Open Source License public class Main { /**//from w ww. j a v a 2 s .co m * From Van Bui - prints out a hex string formatted with * spaces between each hex word of length wordlength. * * @param in input array of bytes to convert * @param wordlength the length of hex words to print otu. */ public static String bytes2HexSplit(byte[] in, int wordlength) { String hex = bytes2Hex(in); StringBuffer buff = new StringBuffer(); for (int i = 0; i < hex.length(); i++) { buff.append(hex.charAt(i)); if ((i + 1) % wordlength == 0) buff.append(" "); } return buff.toString(); } /** * From Van Bui - prints out a hex string formatted with * spaces between each hex word of length wordlength, and * new lines every linelength. * * @param in input array of bytes to convert * @param wordlength the length of hex words to print otu. * @param linelength the length of a line to print before inserting * a line feed. */ public static String bytes2HexSplit(byte[] in, int wordlength, int linelength) { String hex = bytes2Hex(in); StringBuffer buff = new StringBuffer(); for (int i = 0; i < hex.length(); i++) { buff.append(hex.charAt(i)); if ((i + 1) % wordlength == 0) buff.append(" "); if ((i + 1) % linelength == 0) buff.append("\n"); } return buff.toString(); } public static String bytes2Hex(byte[] bytes) { StringBuffer ret = new StringBuffer(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { ret.append(byte2Hex(bytes[i])); } return ret.toString(); } static public String byte2Hex(byte b) { // Returns hex String representation of byte b final char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] }; return new String(array); } }