Here you can find the source of toHexText(byte[] bytes, int length)
Parameter | Description |
---|---|
bytes | the array of bytes. If null, the text "null" is returned. |
length | length up to the bytes should be converted into hexadecimal text. If larger then the array length, reduce it to the array length. |
public static String toHexText(byte[] bytes, int length)
//package com.java2s; /******************************************************************************* * Copyright (c) 2015 Institute for Pervasive Computing, ETH Zurich and others. * //ww w . j a v a 2 s . co m * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v1.0 which accompany this distribution. * * The Eclipse Public License is available at * http://www.eclipse.org/legal/epl-v10.html * and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.html. * * Contributors: * Matthias Kovatsch - creator and main architect * Martin Lanter - architect and re-implementation * Dominique Im Obersteg - parsers and initial implementation * Daniel Pauli - parsers and initial implementation * Kai Hudalla - logging * Achim Kraus (Bosch Software Innovations GmbH) - add toHexText * (for message tracing) ******************************************************************************/ public class Main { /** * Converts the specified byte array up to the specified length into a hexadecimal text. * Separate bytes by spaces and group them in lines. Append length of array, if specified * length is smaller then the length of the array. * * @param bytes the array of bytes. If null, the text "null" is returned. * @param length length up to the bytes should be converted into hexadecimal text. * If larger then the array length, reduce it to the array length. * @return byte array as hexadecimal text */ public static String toHexText(byte[] bytes, int length) { if (bytes == null) return "null"; if (length > bytes.length) length = bytes.length; StringBuilder sb = new StringBuilder(); if (16 < length) sb.append('\n'); for (int index = 0; index < length; ++index) { sb.append(String.format("%02x", bytes[index] & 0xFF)); if (31 == (31 & index)) { sb.append('\n'); } else { sb.append(' '); } } if (length < bytes.length) { sb.append(" .. ").append(bytes.length).append(" bytes"); } return sb.toString(); } }