Android String to Hex Byte Array Convert hexDecode(String data)

Here you can find the source of hexDecode(String data)

Description

hex Decode

Declaration

public static byte[] hexDecode(String data) 

Method Source Code

//package com.java2s;

public class Main {
    public static byte[] hexDecode(String data) {
        byte[] r = new byte[data.length() / 2];

        char[] a = data.toCharArray();

        for (int i = 0; i < a.length; i += 2) {
            char c1 = a[i], c2 = a[i + 1];
            int v1 = valueOf(c1);
            int v2 = valueOf(c2);
            r[i >> 1] = (byte) ((((v1 & 0xf) << 4) | (v2 & 0xf)) & 0xff);
        }// www .  j  a  v  a2s.c o  m

        return r;
    }

    private static int valueOf(char c) {
        c = Character.toLowerCase(c);
        if (c >= '0' && c <= '9') {
            return c - '0';
        } else if (c >= 'a' && c <= 'f') {
            return c - 'a' + 10;
        }
        return -1;
    }
}

Related

  1. stringToHex(String ids)
  2. hexStringToBytes(String s)
  3. hexStringToBytes(String hexString, int offset, int count)
  4. fromHex(String dataString)
  5. fromHexString(String s)