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;
/************************************************************************
 * This file is part of jsnap.                                          *
 *                                                                      *
 * jsnap 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.                                  *
 *                                                                      *
 * jsnap 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 jsnap.  If not, see <http://www.gnu.org/licenses/>.       *
 ************************************************************************/

public class Main {
    public static String toHexString(byte[] b) {
        int n;//from   w  ww .j a va  2 s  .  c  o  m
        String result = "";
        for (byte x : b) {
            n = (x & 0xF0) >> 4;
            result += nibbleToChar(n);
            n = x & 0x0F;
            result += nibbleToChar(n);
        }
        return result;
    }

    private static char nibbleToChar(int n) {
        switch (n & 0x0F) {
        case 1:
            return '1';
        case 2:
            return '2';
        case 3:
            return '3';
        case 4:
            return '4';
        case 5:
            return '5';
        case 6:
            return '6';
        case 7:
            return '7';
        case 8:
            return '8';
        case 9:
            return '9';
        case 10:
            return 'A';
        case 11:
            return 'B';
        case 12:
            return 'C';
        case 13:
            return 'D';
        case 14:
            return 'E';
        case 15:
            return 'F';
        default:
            return '0';
        }
    }
}

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)