Java Hex Convert To fromHex(final CharSequence s)

Here you can find the source of fromHex(final CharSequence s)

Description

from Hex

License

Open Source License

Declaration

public final static int fromHex(final CharSequence s) 

Method Source Code

//package com.java2s;
/**//www  .  ja v  a2s.co  m
 * The contents of this file are subject to the Regenstrief Public License
 * Version 1.0 (the "License"); you may not use this file except in compliance with the License.
 * Please contact Regenstrief Institute if you would like to obtain a copy of the license.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) Regenstrief Institute.  All Rights Reserved.
 */

public class Main {
    public final static int fromHex(final char c) {
        if ((c >= '0') && (c <= '9')) {
            return c - '0';
        } else if (c >= 'a' && c <= 'f') {
            return c - 'a' + 10;
        } else if (c >= 'A' && c <= 'F') {
            return c - 'A' + 10;
        }
        throw new IllegalArgumentException("Invalid hex character " + c);
    }

    public final static int fromHex(final char c1, final char c2) {
        return fromHex(c1) * 16 + fromHex(c2);
    }

    public final static int fromHex(final CharSequence s) {
        return fromHex(s, 0, length(s));
    }

    public final static int fromHex(final CharSequence s, final int off, final int len) {
        int r = 0;
        for (int i = off; i < len; i++) {
            r = (r * 16) + fromHex(s.charAt(i));
        }
        return r;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final Object array) {
        if (array == null) {
            return 0;
        } else if (array instanceof byte[]) {
            return ((byte[]) array).length;
        } else if (array instanceof short[]) {
            return ((short[]) array).length;
        } else if (array instanceof int[]) {
            return ((int[]) array).length;
        } else if (array instanceof long[]) {
            return ((long[]) array).length;
        } else if (array instanceof float[]) {
            return ((float[]) array).length;
        } else if (array instanceof double[]) {
            return ((double[]) array).length;
        } else if (array instanceof boolean[]) {
            return ((boolean[]) array).length;
        } else if (array instanceof char[]) {
            return ((char[]) array).length;
        }
        return ((Object[]) array).length;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final Object[] array) {
        return array == null ? 0 : array.length;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final byte[] array) {
        return array == null ? 0 : array.length;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final int[] array) {
        return array == null ? 0 : array.length;
    }

    /**
     * Retrieves the length of the CharSequence
     * 
     * @param s the CharSequence
     * @return the length
     **/
    public final static int length(final CharSequence s) {
        return s == null ? 0 : s.length();
    }
}

Related

  1. fromHex(byte[] hex)
  2. fromHex(char c)
  3. fromHex(char c)
  4. fromHex(char hi, char lo)
  5. fromHex(CharSequence cs)
  6. fromHex(final String hex)
  7. fromHex(final String hex)
  8. fromHex(final String hexValue)
  9. fromHex(final String s)