Java Hex Calculate toHexString(byte b)

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

Description

to Hex String

License

Apache License

Declaration

public static String toHexString(byte b) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String toHexString(byte b) {
        return toHexString(new byte[] { b }, 0, 1);
    }/*from w  w  w.  j  a  v a  2s  .co  m*/

    public static String toHexString(byte[] b, int offset, int length) {
        StringBuilder buf = new StringBuilder();
        for (int i = offset; i < offset + length; i++) {
            int bi = 0xff & b[i];
            int c = '0' + (bi / 16) % 16;
            if (c > '9')
                c = 'A' + (c - '0' - 10);
            buf.append((char) c);
            c = '0' + bi % 16;
            if (c > '9')
                c = 'a' + (c - '0' - 10);
            buf.append((char) c);
        }
        return buf.toString();
    }

    public static String toString(byte[] bytes, int base) {
        StringBuilder buf = new StringBuilder();
        for (byte b : bytes) {
            int bi = 0xff & b;
            int c = '0' + (bi / base) % base;
            if (c > '9')
                c = 'a' + (c - '0' - 10);
            buf.append((char) c);
            c = '0' + bi % base;
            if (c > '9')
                c = 'a' + (c - '0' - 10);
            buf.append((char) c);
        }
        return buf.toString();
    }
}

Related

  1. toHexStr(byte[] binary)
  2. toHexStream(byte[] bytes, String delimiter, boolean prefixEachValue, boolean upperCase)
  3. toHexString(byte a)
  4. toHexString(byte abyte0[], boolean spaceFlag)
  5. toHexString(byte b)
  6. toHexString(byte b)
  7. toHexString(byte b)
  8. toHexString(byte b)
  9. toHexString(byte b)