Here you can find the source of toHex(byte[] b)
Parameter | Description |
---|---|
b | a parameter |
public static String toHex(byte[] b)
//package com.java2s; /*//from ww w. j av a2 s .co m ************************************************************************ ******************* CANADIAN ASTRONOMY DATA CENTRE ******************* ************** CENTRE CANADIEN DE DONN?ES ASTRONOMIQUES ************** * * (c) 2009. (c) 2009. * Government of Canada Gouvernement du Canada * National Research Council Conseil national de recherches * Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6 * All rights reserved Tous droits r?serv?s * * NRC disclaims any warranties, Le CNRC d?nie toute garantie * expressed, implied, or ?nonc?e, implicite ou l?gale, * statutory, of any kind with de quelque nature que ce * respect to the software, soit, concernant le logiciel, * including without limitation y compris sans restriction * any warranty of merchantability toute garantie de valeur * or fitness for a particular marchande ou de pertinence * purpose. NRC shall not be pour un usage particulier. * liable in any event for any Le CNRC ne pourra en aucun cas * damages, whether direct or ?tre tenu responsable de tout * indirect, special or general, dommage, direct ou indirect, * consequential or incidental, particulier ou g?n?ral, * arising from the use of the accessoire ou fortuit, r?sultant * software. Neither the name de l'utilisation du logiciel. Ni * of the National Research le nom du Conseil National de * Council of Canada nor the Recherches du Canada ni les noms * names of its contributors may de ses participants ne peuvent * be used to endorse or promote ?tre utilis?s pour approuver ou * products derived from this promouvoir les produits d?riv?s * software without specific prior de ce logiciel sans autorisation * written permission. pr?alable et particuli?re * par ?crit. * * This file is part of the Ce fichier fait partie du projet * OpenCADC project. OpenCADC. * * OpenCADC is free software: OpenCADC est un logiciel libre ; * you can redistribute it and/or vous pouvez le redistribuer ou le * modify it under the terms of modifier suivant les termes de * the GNU Affero General Public la ?GNU Affero General Public * License as published by the License?? telle que publi?e * Free Software Foundation, par la Free Software Foundation * either version 3 of the : soit la version 3 de cette * License, or (at your option) licence, soit (? votre gr?) * any later version. toute version ult?rieure. * * OpenCADC is distributed in the OpenCADC est distribu? * hope that it will be useful, dans l?espoir qu?il vous * but WITHOUT ANY WARRANTY; sera utile, mais SANS AUCUNE * without even the implied GARANTIE : sans m?me la garantie * warranty of MERCHANTABILITY implicite de COMMERCIALISABILIT? * or FITNESS FOR A PARTICULAR ni d?AD?QUATION ? UN OBJECTIF * PURPOSE. See the GNU Affero PARTICULIER. Consultez la Licence * General Public License for G?n?rale Publique GNU Affero * more details. pour plus de d?tails. * * You should have received Vous devriez avoir re?u une * a copy of the GNU Affero copie de la Licence G?n?rale * General Public License along Publique GNU Affero avec * with OpenCADC. If not, see OpenCADC ; si ce n?est * <http://www.gnu.org/licenses/>. pas le cas, consultez : * <http://www.gnu.org/licenses/>. * * $Revision: 4 $ * ************************************************************************ */ public class Main { private static final char[] HEXDIGIT = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /** * Encode arbitrary byte array as a hex string. There are always 2 * hex characters per byte in the array. * * @param b * @return a hexadecimal representation of b */ public static String toHex(byte[] b) { StringBuffer sb = new StringBuffer(2 * b.length); for (int i = 0; i < b.length; i++) sb.append(toHex(b[i])); return sb.toString(); } /** * Writes value as a 1-byte hex string (##). The * returned string always has 2 hex digits. * Note: Take on code from Sun/IBM. * * @param b * @return a hexadecimal representation of b */ static public String toHex(byte b) { // Returns hex String representation of byte b char[] array = { HEXDIGIT[(b >> 4) & 0x0f], HEXDIGIT[b & 0x0f] }; return new String(array); } /** * Writes value as a 2-byte hex string (####). The * returned string always has 4 hex digits. * * @param val * @return a hexadecimal representation of val */ public static String toHex(short val) { //if (val < 0) // throw new IllegalArgumentException("can't convert negative value: "+val); // there is no Short.toHexString() String hex = Integer.toHexString(val); if (hex.length() > 4) hex = hex.substring(4); switch (hex.length()) { case 1: return "000" + hex; case 2: return "00" + hex; case 3: return "0" + hex; case 4: return hex; default: throw new NumberFormatException("Integer.toHexString(short) returned " + hex + " characters: " + hex); } } /** * Writes value as a 4-byte hex string (########). The * returned string always has 8 hex digits. * * @param val * @return a hexadecimal representation of val */ public static String toHex(int val) { String hex = Integer.toHexString(val); int c = hex.length(); if (c < 1 || c > 8) throw new NumberFormatException( "Integer.toHexString(int) returned " + hex.length() + " characters: " + hex); StringBuffer sb = new StringBuffer(8); for (int i = 0; i < 8 - c; i++) sb.append("0"); sb.append(hex); return sb.toString(); } /** * Writes value as a 8-byte hex string (################). * The returned string always has 16 hex digits. * * @param val * @return a hexadecimal representation of val */ public static String toHex(long val) { String hex = Long.toHexString(val); int c = hex.length(); if (c < 1 || c > 16) throw new NumberFormatException( "Long.toHexString(long) returned " + hex.length() + " characters: " + hex); StringBuffer sb = new StringBuffer(16); for (int i = 0; i < 16 - c; i++) sb.append("0"); sb.append(hex); return sb.toString(); } }