Java String to Byte Array asBytes(String hexStr)

Here you can find the source of asBytes(String hexStr)

Description

as Bytes

License

Open Source License

Declaration

public static byte[] asBytes(String hexStr) 

Method Source Code

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

public class Main {

    public static byte[] asBytes(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        }//w  w  w  .ja v  a2s .co m

        byte[] result = new byte[hexStr.length() / 2];
        int high, low;
        for (int i = 0; i < hexStr.length() / 2; i++) {
            high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }

        return result;
    }
}

Related

  1. asBytes(String basicString)
  2. asBytes(String hex)
  3. convertStringToByte(String input)
  4. convertStringToByte(String strValue)
  5. convertStringToByteArray(String input)
  6. convertStringToByteArray(String string)