Java Base Encode toBase26(int number)

Here you can find the source of toBase26(int number)

Description

Convert a number into base-26 using lower case letters.

License

Open Source License

Parameter

Parameter Description
number a non-negative number

Exception

Parameter Description
NumberFormatException if the number is negative

Return

a string representing the number in base 26 using lower case

Declaration

public static String toBase26(int number) throws NumberFormatException 

Method Source Code

//package com.java2s;
/*/*from   w  w  w .  ja  va 2  s. co  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 number into base-26 using lower case letters. Negative numbers
     * are not supported; only the magnitude in base-26 will be returned.
     * Adapted from http://en.wikipedia.org/wiki/Hexavigesimal.
     *
     * @param number a non-negative number
     * @return a string representing the number in base 26 using lower case
     * @throws NumberFormatException if the number is negative
     */
    public static String toBase26(int number) throws NumberFormatException {
        if (number < 0)
            throw new NumberFormatException("Negative number");
        number = Math.abs(number);
        String converted = "";
        // Repeatedly divide the number by 26 and convert the
        // remainder into the appropriate letter.
        do {
            int remainder = number % 26;
            converted = (char) (remainder + 'a') + converted;
            number = (number - remainder) / 26;
        } while (number > 0);
        return converted;
    }
}

Related

  1. toBase10SuffixedString(long n)
  2. toBase16(byte[] data)
  3. toBase16(int[] arr)
  4. ToBase16(StringBuilder str, byte[] data)
  5. toBase2(byte b)
  6. toBase2SuffixedString(long n)
  7. toBase32Char(int i)
  8. toBase36(int decimalNumber)
  9. toBase36(long l)