All Downloads are FREE. Search and download functionalities are using the official Maven repository.

common.HEX Maven / Gradle / Ivy

Go to download

This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi

There is a newer version: 42761
Show newest version
package com.unbound.common;

public final class HEX
{
  public static final char[] chars   = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  public static final char[] upChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

  public static String toString(byte[] in)
  {
    char[] c = new char[in.length*2];
    for (int i=0; i>4) & 0x0f];
      c[i*2+1] = chars[in[i] & 0x0f];
    }
    return new String(c);
  }

  public static String toString(long in)
  {
    char[] c = new char[16];
    c[ 0] = chars[(int)(in >> 60) & 0x0f];
    c[ 1] = chars[(int)(in >> 56) & 0x0f];
    c[ 2] = chars[(int)(in >> 52) & 0x0f];
    c[ 3] = chars[(int)(in >> 48) & 0x0f];
    c[ 4] = chars[(int)(in >> 44) & 0x0f];
    c[ 5] = chars[(int)(in >> 40) & 0x0f];
    c[ 6] = chars[(int)(in >> 36) & 0x0f];
    c[ 7] = chars[(int)(in >> 32) & 0x0f];
    c[ 8] = chars[(int)(in >> 28) & 0x0f];
    c[ 9] = chars[(int)(in >> 24) & 0x0f];
    c[10] = chars[(int)(in >> 20) & 0x0f];
    c[11] = chars[(int)(in >> 16) & 0x0f];
    c[12] = chars[(int)(in >> 12) & 0x0f];
    c[13] = chars[(int)(in >>  8) & 0x0f];
    c[14] = chars[(int)(in >>  4) & 0x0f];
    c[15] = chars[(int)(in)       & 0x0f];
    return new String(c);
  }

  public static String toString(int in)
  {
    char[] c = new char[8];
    c[0] = chars[(in >> 28) & 0x0f];
    c[1] = chars[(in >> 24) & 0x0f];
    c[2] = chars[(in >> 20) & 0x0f];
    c[3] = chars[(in >> 16) & 0x0f];
    c[4] = chars[(in >> 12) & 0x0f];
    c[5] = chars[(in >>  8) & 0x0f];
    c[6] = chars[(in >>  4) & 0x0f];
    c[7] = chars[(in)       & 0x0f];
    return new String(c);
  }

  public static String toString(short in)
  {
    char[] c = new char[4];
    c[0] = chars[(in >> 12) & 0x0f];
    c[1] = chars[(in >>  8) & 0x0f];
    c[2] = chars[(in >>  4) & 0x0f];
    c[3] = chars[(in)       & 0x0f];
    return new String(c);
  }

  public static String toString(byte in)
  {
    char[] c = new char[2];
    c[0] = chars[(in >>  4) & 0x0f];
    c[1] = chars[(in)       & 0x0f];
    return new String(c);
  }

  public static int from(char c)
  {
    if (c>='0' && c<='9') return c-'0';
    if (c>='a' && c<='f') return c-'a'+10;
    if (c>='A' && c<='F') return c-'A'+10;
    throw new IllegalArgumentException("Hex string convertion error");
  }

  public static byte[] from(String in)
  {
    if ((in.length() & 1)!=0) throw new IllegalArgumentException("Invalid hex string length");
    int length = in.length() / 2;
    byte[] out = new byte[length];
    for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy