Java Integer Create toInt(String string)

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

Description

to Int

License

Open Source License

Declaration

public static int toInt(String string) 

Method Source Code

//package com.java2s;
/*/*from  w w w .j a  v  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 int toInt(String string) {
        assert string.length() == 4;
        int integer = 0;
        for (int i = 0; i < string.length(); i++) {
            byte tmp = decodeHexChar(string.charAt(i));
            integer <<= 4;
            integer |= tmp;
        }
        return integer;
    }

    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. toInt(String str)
  2. toInt(String str, int defaultValue)
  3. toInt(String str, int defaultValue)
  4. toInt(String str, int defValue)
  5. toInt(String str, int val)
  6. ToInt(String string)
  7. toInt(String string)
  8. toInt(String string)
  9. toInt(String string)