Java tutorial
//package com.java2s; /* * Copyright 2006-2015 WebPKI.org (http://webpki.org). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ public class Main { public static String toHexString(byte[] value, int startOffset, int maxLength, boolean uppercase, char separator) { if (maxLength == -1 || startOffset + maxLength > value.length) { maxLength = value.length - startOffset; } StringBuffer r = new StringBuffer(maxLength * (separator == -1 ? 2 : 3)); for (int i = 0; i < maxLength; i++) { if (i > 0 && separator != 0) { r.append(separator); } String t = Integer.toHexString(value[i + startOffset] & 0xFF); if (t.length() == 1) { t = "0" + t; } if (uppercase) { t = t.toUpperCase(); } r.append(t); } return r.toString(); } public static String toHexString(byte[] value, int startOffset, int maxLength) { return toHexString(value, startOffset, maxLength, true, ' '); } public static String toHexString(byte[] value) { return toHexString(value, 0, -1, true, ' '); } public static String toHexString(int value, char byteSeparator) { return toHexString(new byte[] { (byte) ((value >> 24) & 0xFF), (byte) ((value >> 16) & 0xFF), (byte) ((value >> 8) & 0xFF), (byte) (value & 0xFF) }, 0, -1, true, byteSeparator); } }