Jump to content

Base 26: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Yaz (talk | contribs)
Add python example of the base 26 encoding and decoding.
Yaz (talk | contribs)
Add missing case for encoding 0 as A in the python example
Line 70: Line 70:
numerals="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numerals="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
n=int(args[1])
n=int(args[1])
if n == 0:
retval="A"
while n > 0:
while n > 0:
retval=numerals[n%26] + retval
retval=numerals[n%26] + retval

Revision as of 08:10, 22 January 2012

A hexavigesimal numeral system has a base of twenty-six.

Base-26 may be represented by using conventional numerals for the digits 0 to 9, and then the letters A to P for the tenth to twenty-fifth digits. "10" would represent 26, "11" = 27, "AB" = 271 and "NP" = 623.

Alternatively, base-26 may be represented using only letters of the Latin alphabet. As there are 26 letters in English, base-26 is also the highest base in which this is possible and hence utilizes every letter. 0 is represented by A, 1 = B, 2 = C ... 24 = Y, 25 = Z. Some examples: 26 = BA, 678 = BAC.

These systems are of limited practical value, although letters used in nominal or serial numbers can be thought as hexavigesimal numerals for calculation purposes if the entire alphabet is used.

Fractions

The fact that 26 is a composite number and lies between two composite numbers (25 and 27) leads to many simple fractions.

B/C = A.N
B/D = A.IRIRIRIR...
B/E = A.GN
B/F = A.FFFFFFF...

The fractions B/G, B/I, B/J, B/K, B/M, B/N, B/P, B/Q are also simple.

Conversion algorithm (alphabet-only system)

Any number may be converted to base-26 by repeatedly dividing the number by 26. The remainders of each division will be the base-26 digits from right to left (least-significant to most-significant place). For example, to convert 678 to "BAC", the first division yields 26 remainder 2, so 2 (C) is the last digit. The quotient 26 is divided again, yielding 1 remainder 0, so 0 (A) is the second-last digit. The next quotient 1 is then divided to give 0 remainder 1, so the final digit is 1 (B). This is extensible to fractions.

This algorithm may be represented in Java to convert a non-negative integer to a base-26 character string as follows:

    public static String toBase26(int 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;
    }

The reverse conversion is achieved by processing each base-26 digit from left to right. The value of the first (leftmost) digit is multiplied by 26 and then added to the subsequent digit. If digits remain, then the cumulative sum is multiplied by 26 before adding the next digit, and so on. Note that this works for any base as long as one has the tools to perform multiplication by 26 and addition in that base. For example, to convert "BAC" to 678, B (1) is multiplied to give 26 and added to A (0) to yield 26. This is multiplied to give 676 and added to C (2) to yield 678.

The reverse conversion algorithm may be represented in Java to convert a base-26 character string to an integer as follows:

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

Python conversion algorithm

This python example does both encoding and decoding. It reads the first argument from the command line as input. If the input begins with a digit, it decodes it to alphabets, otherwise it encodes into a number. The advantage over the C/C++ is the long numeric values Python can handle.

import sys

def main(*args):
        try:
                n=ord(args[1][0])
                if n > 47 and n < 58:
                        retval=""
                        numerals="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                        n=int(args[1])
                        if n == 0:
                                retval="A"
                        while n > 0:
                                retval=numerals[n%26] + retval
                                n = n / 26
                        print retval
                else:
                        retval=0
                        for c in args[1]:
                                n=ord(c)-65
                                if n > -1 and n < 27:
                                        retval=retval*26+n
                        print retval
        except:
                print "exception"
        else:
                return 0

if __name__ == '__main__':
        sys.exit(main(*sys.argv))

John Nash

While at Princeton University in the 1970s, mathematician John Nash worked extensively in base-26 as part of his obsession with numerology and the uncovering of "hidden" messages. Nash composed letters and other messages in which the actual English text also represented a base-26 equation, the answer to which would itself be both a base-26 number and a word or phrase in English. Nash is believed to have devised algorithms with which he was able to adapt primitive electronic calculators to assist him in performing hexavigesimal calculations.[1]

Bijective base 26

Using bijective numeration, it is possible to operate in base 26 without a zero; it uses digits "A" to "Z" to represent one to twenty-six and has no zero. Many spreadsheets including Microsoft Excel use the 26-adic counting system with the "digits" A-Z to label the columns of a spreadsheet, starting A, B, C... Z, AA, AB... AZ, BA... ZZ, AAA, etc. A variant of this system is used to name variable stars.

The following algorithm (in Java) will allow you to convert a 0 based number string to a Bijective Base 26 number. This means that 0 = A, 26 = AA, 676 = AAA and so forth.

   public static String toBase26(int value) {

   // Note: This is a slightly modified version of the Alphabet-only conversion algorithm
		
   value = Math.abs(value);
   String converted = "";

   boolean iteration = false;
        
   // Repeatedly divide the number by 26 and convert the
   // remainder into the appropriate letter.
   do {
      int remainder = value % 26;
        	
      // Compensate for the last letter of the series being corrected on 2 or more iterations.
      if (iteration && value < 25) {
         remainder--;
      }
        	
      converted = (char)(remainder + 'A') + converted;
      value = (value - remainder) / 26;
            
      iteration = true;
   } while (value > 0);
 
   return converted;	
}

References

  1. ^ Nasar, Sylvia (2001). A Beautiful Mind. Simon and Schuster. pp. 333–6. ISBN 0743224574.