Java Hex Calculate toHexString(byte[] b)

Here you can find the source of toHexString(byte[] b)

Description

to Hex String

License

Open Source License

Declaration

public static String toHexString(byte[] b) 

Method Source Code

//package com.java2s;
/*//  w  w  w.j  ava  2 s  . c o  m
 * HexUtils.java
 *
 * Copyright (c) 2009 Ruben Laguna <ruben.laguna at gmail.com>. All rights reserved.
 *
 * This file is part of XBeeApplication.
 *
 * XBeeApplication is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XBeeApplication is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XBeeApplication.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    public static String toHexString(byte[] b) {
        if (b == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            byte bb = b[i];
            String oneByteString = toHexString(bb);
            sb.append(oneByteString);
            sb.append(" ");
            if (((i + 1) % 20) == 0) {
                sb.append("\n");
            }
        }
        return sb.toString();
    }

    public static String toHexString(int[] b) {
        byte[] tmp = new byte[b.length];
        for (int i = 0; i < b.length; i++) {
            tmp[i] = (byte) b[i];

        }
        return toHexString(tmp);
    }

    public static String toHexString(byte bb) {
        StringBuffer oneByteBuffer = new StringBuffer();
        // look up high nibble char
        oneByteBuffer.append(hexChar[(bb & 0xf0) >>> 4]);
        // look up low nibble char
        oneByteBuffer.append(hexChar[bb & 0x0f]);
        String oneByteString = oneByteBuffer.toString();
        return oneByteString;
    }
}

Related

  1. toHexString(byte[] b)
  2. toHexString(byte[] b)
  3. toHexString(byte[] b)
  4. toHexString(byte[] b)
  5. toHexString(byte[] b)
  6. toHexString(byte[] b)
  7. toHexString(byte[] b)
  8. toHexString(byte[] b)
  9. toHexString(byte[] b)