Here you can find the source of bin2Hex(byte[] bin)
Parameter | Description |
---|---|
bin | - data in binary for convert |
public static byte[] bin2Hex(byte[] bin)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from w ww.j a v a 2s .co m*/ * Binary byte[] to Heximal byte[] * * @param bin * - data in binary for convert */ public static byte[] bin2Hex(byte[] bin) { // Allocate byte[] for return byte[] hex = new byte[2 * bin.length]; // Convert one binary byte to two heximal bytes for (int i = 0, j = 0; i < bin.length; i++, j += 2) { int iByte = bin[i]; if (iByte < 0) iByte += 256; // First byte int i4Bit = iByte >> 4; if (i4Bit < 10) hex[j] = (byte) ('0' + i4Bit); else hex[j] = (byte) ('A' + i4Bit - 10); // Second byte i4Bit = iByte & 0x000F; if (i4Bit < 10) hex[j + 1] = (byte) ('0' + i4Bit); else hex[j + 1] = (byte) ('A' + i4Bit - 10); } // Return the heximal byte[] return hex; } }