Java - Write code to Convert a hex-encoded string into a byte in char type.

Requirements

Write code to Convert a hex-encoded string into a byte in char type.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String orgString = "book2s.com";
        System.out.println(hexStringToByteChar(orgString));
    }/*www  .ja  va2  s. c o  m*/

    /**
     * Converts a hex-encoded string into a byte in char type.
     * 
     * @param orgString
     *            a string of an hex-value, e.g. "F0" (0x is not part of this
     *            string)
     * @return the value of the string in byte (as char)
     */
    public static char hexStringToByteChar(String orgString) {
        if (orgString.length() > 2) {
            throw new IllegalArgumentException(
                    "Too long, has to be 2 characters");
        }
        Integer orgInt = Integer.valueOf(orgString, 16);
        char val = (char) orgInt.intValue();
        return val;
    }
}