Java Byte Array Create toBytesFromHexString(String digits)

Here you can find the source of toBytesFromHexString(String digits)

Description

to Bytes From Hex String

License

Open Source License

Declaration

public static byte[] toBytesFromHexString(String digits)
        throws IllegalArgumentException, NumberFormatException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {

    public static byte[] toBytesFromHexString(String digits)
            throws IllegalArgumentException, NumberFormatException {
        if (digits == null) {
            return null;
        }//from ww w  .j a  v  a2s .  com
        int length = digits.length();
        if (length % 2 == 1) {
            throw new IllegalArgumentException("For input string: \"" + digits + "\"");
        }
        length = length / 2;
        byte[] bytes = new byte[length];
        for (int i = 0; i < length; i++) {
            int index = i * 2;
            bytes[i] = (byte) Short.parseShort(digits.substring(index, index + 2), 16);
        }
        return bytes;
    }
}

Related

  1. toBytesDirect(final String singleOctets)
  2. toBytesFromASCII(final char[] chars)
  3. toBytesFromBase64(String inBase64String)
  4. toBytesFromBin(String binSymbols)
  5. toBytesFromHexStr(String hexStr)
  6. toBytesFromOct(String octSymbols)
  7. toBytesFromString(String s)
  8. toBytesFromUnicode(String s)
  9. toBytesHexEscaped(final byte[] s, final int off, final int len)