Java Hex Convert To fromHex(String s)

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

Description

from Hex

License

Apache License

Declaration

public static byte fromHex(String s) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static final String HEX_STRING = "0123456789abcdef";

    public static byte fromHex(String s) {
        if (s.length() != 2) {
            throw new IllegalArgumentException("Invalid length of string.");
        }/* w  w w .j av a 2s.co  m*/
        char c1 = s.charAt(0);
        char c2 = s.charAt(1);
        int n1 = HEX_STRING.indexOf(c1);
        int n2 = HEX_STRING.indexOf(c2);
        if (n1 == (-1)) {
            throw new IllegalArgumentException("Invalid char in string: " + c1);
        }
        if (n2 == (-1)) {
            throw new IllegalArgumentException("Invalid char in string: " + c2);
        }
        int n = (n1 << 4) + n2;
        return (byte) n;
    }
}

Related

  1. fromHex(String hexString)
  2. fromHex(String hexString)
  3. fromHex(String input, int max)
  4. fromHex(String s)
  5. fromHex(String s)
  6. fromHex(String s)
  7. fromHex(String s)
  8. fromHex(String s)
  9. fromHex(String s)