Java - Write code to Convert hex in string To Byte

Requirements

Write code to convert hex To Byte

Hint

Hard code string hex value

Use if statement to check the hex value

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String input = "dead";
        System.out.println(java.util.Arrays.toString(hexToByte(input)));
    }//from w  w  w  .j a  v  a 2 s  .  c om

    public static byte[] hexToByte(String input) {
        byte[] output = new byte[input.length() / 2];
        String input2 = input.toLowerCase();
        for (int i = 0; i < input2.length(); i += 2) {
            output[i / 2] = hexToByte(input2.charAt(i),
                    input2.charAt(i + 1));
        }
        return output;
    }

    private static byte hexToByte(char char1, char char2) {
        // Returns hex String representation of byte b
        byte output = 0x00;
        if (char1 == '0') {
            output = 0x00;
        } else if (char1 == '1') {
            output = 0x10;
        } else if (char1 == '2') {
            output = 0x20;
        } else if (char1 == '3') {
            output = 0x30;
        } else if (char1 == '4') {
            output = 0x40;
        } else if (char1 == '5') {
            output = 0x50;
        } else if (char1 == '6') {
            output = 0x60;
        } else if (char1 == '7') {
            output = 0x70;
        } else if (char1 == '8') {
            output = (byte) 0x80;
        } else if (char1 == '9') {
            output = (byte) 0x90;
        } else if (char1 == 'a') {
            output = (byte) 0xa0;
        } else if (char1 == 'b') {
            output = (byte) 0xb0;
        } else if (char1 == 'c') {
            output = (byte) 0xc0;
        } else if (char1 == 'd') {
            output = (byte) 0xd0;
        } else if (char1 == 'e') {
            output = (byte) 0xe0;
        } else if (char1 == 'f') {
            output = (byte) 0xf0;
        }

        if (char2 == '0') {
            output = (byte) (output | (byte) 0x00);
        } else if (char2 == '1') {
            output = (byte) (output | (byte) 0x01);
        } else if (char2 == '2') {
            output = (byte) (output | (byte) 0x02);
        } else if (char2 == '3') {
            output = (byte) (output | (byte) 0x03);
        } else if (char2 == '4') {
            output = (byte) (output | (byte) 0x04);
        } else if (char2 == '5') {
            output = (byte) (output | (byte) 0x05);
        } else if (char2 == '6') {
            output = (byte) (output | (byte) 0x06);
        } else if (char2 == '7') {
            output = (byte) (output | (byte) 0x07);
        } else if (char2 == '8') {
            output = (byte) (output | (byte) 0x08);
        } else if (char2 == '9') {
            output = (byte) (output | (byte) 0x09);
        } else if (char2 == 'a') {
            output = (byte) (output | (byte) 0x0a);
        } else if (char2 == 'b') {
            output = (byte) (output | (byte) 0x0b);
        } else if (char2 == 'c') {
            output = (byte) (output | (byte) 0x0c);
        } else if (char2 == 'd') {
            output = (byte) (output | (byte) 0x0d);
        } else if (char2 == 'e') {
            output = (byte) (output | (byte) 0x0e);
        } else if (char2 == 'f') {
            output = (byte) (output | (byte) 0x0f);
        }
        return output;
    }
}