Java Base Convert fromBase26(String number)

Here you can find the source of fromBase26(String number)

Description

Convert a base-26 representation into a number, considering it to represent a value in base-26 using the characters a-z or A-Z - the string is treated case-insensitively.

License

Open Source License

Parameter

Parameter Description
number a string representing a base-26 number

Exception

Parameter Description
NumberFormatException if the string does not represent an alphabetical base-26 number

Return

the number represented

Declaration

public static int fromBase26(String number) throws NumberFormatException 

Method Source Code

//package com.java2s;
/*//ww w.j  a v  a 2  s  . c  o m
    
Copyright (c) 2000-2014 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    
Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.
    
*/

public class Main {
    /**
     * Convert a base-26 representation into a number, considering it to represent
     * a value in base-26 using the characters a-z or A-Z - the string is treated
     * case-insensitively. The string is lower cased before the increment is
     * applied. An exception will be thrown if any character is outside of a-z
     * after lower casing.
     * <p>
     * Adapted from http://en.wikipedia.org/wiki/Hexavigesimal.
     * @param number a string representing a base-26 number
     * @return the number represented
     * @throws NumberFormatException if the string does not represent an alphabetical base-26 number
     */
    public static int fromBase26(String number) throws NumberFormatException {
        if (!number.matches("^[a-zA-Z]*$"))
            throw new NumberFormatException("Non-alphabetic characters in string.");

        int s = 0;
        if (number != null && number.length() > 0) {
            number = number.toLowerCase();
            s = (number.charAt(0) - 'a');
            for (int i = 1; i < number.length(); i++) {
                s *= 26;
                s += (number.charAt(i) - 'a');
            }
        }
        return s;
    }
}

Related

  1. fromBase(String num, String baseChars)
  2. fromBase10(final long base10)
  3. fromBase16(String data)
  4. fromBase16toByteArray(String bytes)
  5. fromBase16toIntArray(String bytes)
  6. fromBase36(String base36)
  7. fromBase36(String base36Number)
  8. fromBase58(String input)
  9. fromBase62(String base62Number)