2

I have an ID field that automatically increments by 1 when a new record is added. Rather than use the numeric ID as is, I would like to convert it to a 6 character string made up of 0-9A-Z (no lowercase letters) with the following format:

A00000

The numbers should increment from 0-9 then A-Z.

Examples

If I were to encode the number 7 for example, it should return:

A00007

If I encode the number 13 it should return:

A0000D

If I encode the number 36 it should return:

A00010

Hopefully this makes sense. Any help would be greatly appreciated.

Jason Towne
  • 8,014
  • 5
  • 54
  • 69

2 Answers2

4

Suggestion: Add 604661760 to the integer value. That's the base-10 value of A00000, interpreted as a base-36 number ( = 10 * 36 ^ 5 ).

Then encode the result in base 36.

Depending on your data range, you might want to promote the int to a long before adding 604661760.

phoog
  • 42,068
  • 6
  • 79
  • 117
1

What you have here is conversion from a base 10 number to a base 36 number. Here's an algorithm for conversion from base 10 to base K. Modify it to suit your needs:

http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/toBaseK.html

System Down
  • 6,192
  • 1
  • 30
  • 34