Android CharSequence Convert convertValueToUnsignedInt(String value, int defaultValue)

Here you can find the source of convertValueToUnsignedInt(String value, int defaultValue)

Description

convert Value To Unsigned Int

License

Apache License

Declaration

public static int convertValueToUnsignedInt(String value,
            int defaultValue) 

Method Source Code

//package com.java2s;
/*//from   w  w w.j a v  a 2 s . com
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    public static int convertValueToUnsignedInt(String value,
            int defaultValue) {
        if (null == value) {
            return defaultValue;
        }

        return parseUnsignedIntAttribute(value);
    }

    public static int parseUnsignedIntAttribute(CharSequence charSeq) {
        String value = charSeq.toString();

        long bits;
        int index = 0;
        int len = value.length();
        int base = 10;

        if ('0' == value.charAt(index)) {
            //  Quick check for zero by itself
            if (index == (len - 1))
                return 0;

            char c = value.charAt(index + 1);

            if ('x' == c || 'X' == c) { //  check for hex
                index += 2;
                base = 16;
            } else { //  check for octal
                index++;
                base = 8;
            }
        } else if ('#' == value.charAt(index)) {
            index++;
            base = 16;
        }

        return (int) Long.parseLong(value.substring(index), base);
    }
}

Related

  1. convertValueToInt(CharSequence charSeq, int defaultValue)
  2. convertValueToList(CharSequence value, String[] options, int defaultValue)
  3. convertValueToList(CharSequence value, String[] options, int defaultValue)
  4. convertValueToList(CharSequence value, String[] options, int defaultValue)
  5. convertValueToUnsignedInt(String value, int defaultValue)
  6. convertValueToUnsignedInt(String value, int defaultValue)
  7. convertValueToUnsignedInt(String value, int defaultValue)