Java Byte Create toByte(String string)

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

Description

to Byte

License

Open Source License

Declaration

public static byte toByte(String string) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w . jav a2 s.  c  o  m*/
 * Copyright (c) 2010-2011 Brigham Young University
 * 
 * This file is part of the BYU RapidSmith Tools.
 * 
 * BYU RapidSmith Tools is free software: you may 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.
 * 
 * BYU RapidSmith Tools 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.
 * 
 * A copy of the GNU General Public License is included with the BYU 
 * RapidSmith Tools. It can be found at doc/gpl2.txt. You may also 
 * get a copy of the license at <http://www.gnu.org/licenses/>.
 * 
 */

public class Main {
    public static byte toByte(String string) {
        assert string.length() == 2;
        byte b = (byte) ((byte) (decodeHexChar(string.charAt(0)) << 4) | (byte) (decodeHexChar(string
                .charAt(1))));
        return b;
    }

    private static byte decodeHexChar(char c) {
        byte b;
        switch (c) {
        case 'F':
            b = (byte) 0x0000000F;
            break;
        case 'E':
            b = (byte) 0x0000000E;
            break;
        case 'D':
            b = (byte) 0x0000000D;
            break;
        case 'C':
            b = (byte) 0x0000000C;
            break;
        case 'B':
            b = (byte) 0x0000000B;
            break;
        case 'A':
            b = (byte) 0x0000000A;
            break;
        case '9':
            b = (byte) 0x00000009;
            break;
        case '8':
            b = (byte) 0x00000008;
            break;
        case '7':
            b = (byte) 0x00000007;
            break;
        case '6':
            b = (byte) 0x00000006;
            break;
        case '5':
            b = (byte) 0x00000005;
            break;
        case '4':
            b = (byte) 0x00000004;
            break;
        case '3':
            b = (byte) 0x00000003;
            break;
        case '2':
            b = (byte) 0x00000002;
            break;
        case '1':
            b = (byte) 0x00000001;
            break;
        case '0':
            b = (byte) 0x00000000;
            break;
        default:
            b = (byte) 0x00000000;
            break;
        }
        return b;
    }
}

Related

  1. toByte(String hexStr)
  2. toByte(String hexString)
  3. toByte(String str)
  4. toByte(String string)
  5. toByte(String string)
  6. toByte(String value)
  7. toByte(String value)
  8. toByte(T value)
  9. toByteBE(int value, byte[] dst, int offset)