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;
// Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    private static int intCacheSize = Integer.getInteger("org.openqa.jetty.util.TypeUtil.IntegerCacheSize", 600)
            .intValue();// www .j  a  va 2 s  .  c om
    private static String[] integerStrCache = new String[intCacheSize];

    public static String toHexString(byte[] b) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < b.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 toHexString(byte[] b, int offset, int length) {
        StringBuffer buf = new StringBuffer();
        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();
    }

    /** Convert int to String using cache. 
     */
    public static String toString(int i) {
        if (i >= 0 && i < intCacheSize) {
            if (integerStrCache[i] == null)
                integerStrCache[i] = Integer.toString(i);
            return integerStrCache[i];
        } else if (i == -1)
            return "-1";
        return Integer.toString(i);
    }

    public static String toString(byte[] bytes, int base) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            int bi = 0xff & bytes[i];
            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. toHexString(byte value)
  2. toHexString(byte[] arr)
  3. toHexString(byte[] array)
  4. toHexString(byte[] array)
  5. toHexString(byte[] b)
  6. toHexString(byte[] b)
  7. toHexString(byte[] b)
  8. toHexString(byte[] b)
  9. toHexString(byte[] b)