Java Hex Convert To fromHex(String hex)

Here you can find the source of fromHex(String hex)

Description

from Hex

License

Open Source License

Declaration

public static byte[] fromHex(String hex) 

Method Source Code

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

public class Main {
    public static byte[] fromHex(String hex) {
        byte[] res = new byte[hex.length() / 2];
        for (int i = 0; i < res.length; i++) {
            res[i] = (byte) ((fromHexShort(hex.charAt(i * 2)) << 4) + fromHexShort(hex.charAt(i * 2 + 1)));
        }/* www .  j  a  v a  2  s  .c  o  m*/
        return res;
    }

    private static int fromHexShort(char a) {
        if (a >= '0' && a <= '9') {
            return a - '0';
        }
        if (a >= 'a' && a <= 'f') {
            return 10 + (a - 'a');
        }

        throw new RuntimeException();
    }
}

Related

  1. fromHex(String hex)
  2. fromHex(String hex)
  3. fromHex(String hex)
  4. fromHex(String hex)
  5. fromHex(String hex)
  6. fromHex(String hex)
  7. fromHex(String hex)
  8. fromHex(String hexBytes)
  9. fromHex(String hexData)