Java Byte Create toByte(String hex)

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

Description

returns byte of given hex string expression.

License

Open Source License

Parameter

Parameter Description
hex hex string

Return

byte

Declaration

public static byte toByte(String hex) 

Method Source Code

//package com.java2s;
/* ------------------------------------------------------------------------
 *    Copyright (C) 2015  www.okeydokeyframework.org
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *    //  w  w w . j  a  va 2  s  .c om
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 * ------------------------------------------------------------------------ 
 */

public class Main {
    /**
     * String prefix of hex value
     */
    private static String HEX_PREFIX = "0x";

    /**
     * returns byte of given hex string expression.
     * <pre><blockquote><code>
     * String s1 = "0xff";
     * String s2 = "ff";
     * 
     * byte b1 = HexDumpUtil.toByte(s1);    // b1 = -1
     * byte b2 = HexDumpUtil.toByte(s2);   // b2 = -1
     * </code></blockquote></pre>
     * 
     * @param    hex hex string
     * @return   byte
     */
    public static byte toByte(String hex) {

        if (hex.startsWith(HEX_PREFIX)) {
            hex = hex.replace(HEX_PREFIX, "");
        }

        int i = Integer.parseInt(hex, 16);

        return (byte) i;
    }
}

Related

  1. toByte(Object value)
  2. toByte(short unsignedByte)
  3. toByte(short value, boolean first)
  4. toByte(String hex)
  5. toByte(String hex)
  6. toByte(String hexStr)
  7. toByte(String hexString)
  8. toByte(String str)
  9. toByte(String string)