Java Byte Array Create toByteArray(final String hexString)

Here you can find the source of toByteArray(final String hexString)

Description

Converts the given hex string to byte array

License

Apache License

Parameter

Parameter Description
hexString the hex string to be converted to bytes (which cannot be null )

Exception

Parameter Description
NullPointerException if the given hex string is null

Return

the byte array

Declaration

public static byte[] toByteArray(final String hexString) throws NullPointerException 

Method Source Code

//package com.java2s;
/*/*from www.ja  v  a  2  s . c o  m*/
 * #%L
 * JavaCreed Secure Properties Encoder
 * %%
 * Copyright (C) 2012 - 2015 Java Creed
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

public class Main {
    /**
     * Converts the given hex string to byte array
     *
     * @param hexString
     *          the hex string to be converted to bytes (which cannot be {@code null})
     * @return the byte array
     * @throws NullPointerException
     *           if the given hex string is {@code null}
     */
    public static byte[] toByteArray(final String hexString) throws NullPointerException {
        final int length = hexString.length();
        final byte[] bytes = new byte[length / 2];
        for (int i = 0; i < length; i += 2) {
            bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
                    + Character.digit(hexString.charAt(i + 1), 16));
        }
        return bytes;
    }
}

Related

  1. toByteArray(final int i)
  2. toByteArray(final int i)
  3. toByteArray(final int i, final byte[] output, final int offset)
  4. toByteArray(final int value)
  5. toByteArray(final String bitString)
  6. toByteArray(int data)
  7. toByteArray(int i)
  8. toByteArray(int in)
  9. toByteArray(int in, int size)