Java Hex Convert To fromHexString(String s)

Here you can find the source of fromHexString(String s)

Description

from Hex String

License

Apache License

Declaration

public static byte[] fromHexString(String s) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    public static byte[] fromHexString(String s) {
        if (s.length() % 2 != 0)
            throw new IllegalArgumentException(s);
        byte[] array = new byte[s.length() / 2];
        for (int i = 0; i < array.length; i++) {
            int b = Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16);
            array[i] = (byte) (0xff & b);
        }//from   w  ww .  j  a  v  a 2  s .  c o  m
        return array;
    }

    /** Parse an int from a substring.
     * Negative numbers are not handled.
     * @param s String
     * @param offset Offset within string
     * @param length Length of integer or -1 for remainder of string
     * @param base base of the integer
     * @exception NumberFormatException 
     */
    public static int parseInt(String s, int offset, int length, int base) throws NumberFormatException {
        int value = 0;

        if (length < 0)
            length = s.length() - offset;

        for (int i = 0; i < length; i++) {
            char c = s.charAt(offset + i);

            int digit = c - '0';
            if (digit < 0 || digit >= base || digit >= 10) {
                digit = 10 + c - 'A';
                if (digit < 10 || digit >= base)
                    digit = 10 + c - 'a';
            }
            if (digit < 0 || digit >= base)
                throw new NumberFormatException(s.substring(offset, offset + length));
            value = value * base + digit;
        }
        return value;
    }
}

Related

  1. fromHexString(String s)
  2. fromHexString(String s)
  3. fromHexString(String s)
  4. fromHexString(String s)
  5. fromHexString(String s)
  6. fromHexString(String s)
  7. fromHexString(String s)
  8. fromHexString(String s, int offset, int length)
  9. fromHexString(String str)