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;
/*/*from  w  w  w  . j  av  a2s.  c  o  m*/
 * Copyright (C) 2014 Valley Campus Japan, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String toHexString(byte b) {
        String hex = Integer.toHexString(b);
        int len = hex.length();
        if (len == 2) {
            return hex;
        } else if (len == 1) {
            return "0" + hex;
        } else {
            return hex.substring(len - 2);
        }
    }

    public static String toHexString(short s) {
        return toHexString((byte) (s >> 8)) + toHexString((byte) s);
    }

    public static String toHexString(byte[] data, int offset, int length) {
        String s = "";
        for (int i = offset; i < offset + length; i++) {
            s += toHexString(data[i]);
        }
        return s;
    }
}

Related

  1. toHexString(byte a)
  2. toHexString(byte abyte0[], boolean spaceFlag)
  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)