# Incrementing alpha characters. Base36

• 07-18-2006
chops11
Incrementing alpha characters. Base36
Afternoon all,

Came upon a problem needed at work where i'm having some trouble programming the solution. They are looking for a simple program which takes entry of a 6 digit "number" as a START number, then another 6 digit "number" as an ending number. The program needs to generate the numbers inbetween (inclusive of the start and end).

For example:

Start 100000
End 100011

would generate:

100000
100001
100002
100003
100004
100005
100006
100007
100008
100009
100010
100011

I wrote this without any problems several months ago. The trouble now is, they specifications have changed. The last 4 digits of the 6 digit number now will no longer be base10, but base 36. So for example:

Start: 100000
End: 100011

Needs to generate:

would generate:

100000
100001
100002
100003
100004
100005
100006
100007
100008
100009
10000A
10000B
10000C
...
10000X
10000Z
100010
100011

I'm having trouble on how to accomplish this. My first though was to handle each of the last 4 characters individually, increment the ones digit from 0-Z, then increment the tens digit and so on, however, that seems very not efficient.

• 07-18-2006
twomers
If you have the number saved as a char*, you can use an atoi() function, and change the base to 36. i think it goes that far anyway. or something. Look into it.
• 07-18-2006
DougDbug
The trick to working with different bases is this: Conversion is done only during input/output. Once the number exists as a C++ integer, the base doesn't matter. The integer is going to be stored in binary. By default, C++ automatically assumes that input/output are decimal. Hex and octal are also built-into cin and cout.

There is a standard C function called strtol() which can convert a string representation in any base to a type-long. There is no standard function to convert the long back to a string, but the Microsoft compiler has _itoa().

Quote:

I'm having trouble on how to accomplish this. My first though was to handle each of the last 4 characters individually, increment the ones digit from 0-Z, then increment the tens digit and so on, however, that seems very not efficient.
In any case, you will need to separate the last four digits, because the full 6-digit "number" is a hybrid of decimal and base-36.

Code fragment...
Code:

```    int Base, x ;     char InputString[40];     char OutputString[40];      // For itoa()  [Microsoft only]     char *pEnd = NULL;          // Required for Strtoul()         cout << "Base? (2-36, 0 to exit) " ;         cin >> Base;                 if(!Base)             return 0;           cout << "Number? ";           cin >> InputString;           x = (int)strtol(InputString, &pEnd, Base);    // String to long           //*** MICROSOFT ONLY - NOT ANSI C++ ******************************         // Comment-out this section if it causes an error on your compiler         _itoa(x, OutputString, Base);         cout << "\t" << OutputString << "\t\t Base-" << dec << Base <<  endl; //*** MICROSOFT ONLY - NOT ANSI C++ ******************************                 cout << "\t" << dec << x << "\t\t Decimal" << endl;         cout << "\t" << oct << x << "\t\t Octal" << endl;         cout << "\t" << hex << x << "\t\t Hex" << endl;```
• 07-18-2006
Ancient Dragon
>>There is no standard function to convert the long back to a string

sprintf() is a standard C function and will do that. (Yet another reason for C superiority over C++ streams :)
• 07-18-2006
DougDbug
Quote:

sprintf() is a standard C function and will do that. (Yet another reason for C superiority over C++ streams
In base-36?????
• 07-18-2006
Ancient Dragon
>>In base-36?????

No -- not like strtol(). Maybe I misunderstood your statement, I thought you were just making a general statement that there is no standard c function to convert a long into a string.