Android Hex String Create hexStringToBytes(String hexString)

Here you can find the source of hexStringToBytes(String hexString)

Description

Convert hex string to byte[]

Parameter

Parameter Description
hexString the hex string

Return

byte[]

Declaration

@SuppressLint("DefaultLocale")
public static byte[] hexStringToBytes(String hexString) 

Method Source Code

//package com.java2s;

import android.annotation.SuppressLint;

public class Main {
    /**//from w w  w  . jav a2  s.c o  m
     * Convert hex string to byte[]
     * 
     * @param hexString
     *            the hex string
     * @return byte[]
     */
    @SuppressLint("DefaultLocale")
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int byteArrayLength = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();

        byte[] d = new byte[byteArrayLength];
        for (int i = 0; i < byteArrayLength; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     * Convert char to byte
     * 
     * @param c
     *            char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
}

Related

  1. getHexString(int i)
  2. isHex(String sampleData)
  3. isHexStringChar(char c)
  4. FileNamePathExtension(String filename)
  5. pathExtension(String fileName)
  6. appendHex(StringBuffer stringbuffer, byte byte0)
  7. toHexString(char achar0)
  8. decodeHexStr(final String str)
  9. hexDigest(String input)