Java Hex Calculate toHexStr(byte[] binary)

Here you can find the source of toHexStr(byte[] binary)

Description

to Hex Str

License

MIT License

Declaration

public static String toHexStr(byte[] binary) 

Method Source Code

//package com.java2s;
/*!/*from  ww w  .  ja v  a 2s.  co  m*/
 * mifmi-commons4j
 * https://github.com/mifmi/mifmi-commons4j
 *
 * Copyright (c) 2015 mifmi.org and other contributors
 * Released under the MIT license
 * https://opensource.org/licenses/MIT
 */

public class Main {
    public static String toHexStr(byte[] binary) {
        if (binary == null) {
            return null;
        }
        if (binary.length == 0) {
            return "";
        }

        int len = binary.length;

        StringBuilder sb = new StringBuilder(len * 2);
        for (byte b : binary) {
            int high = ((b >>> 4) & 0xF);
            int low = (b & 0xF);
            if (high < 10) {
                sb.append((char) ('0' + high));
            } else {
                sb.append((char) ('A' + (high - 10)));
            }
            if (low < 10) {
                sb.append((char) ('0' + low));
            } else {
                sb.append((char) ('A' + (low - 10)));
            }
        }
        return sb.toString();
    }
}

Related

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