Thread: Incrementing alpha characters. Base36

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    32

    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.


    So, any advice would be terrific. Thanks in advance.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    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.
    Last edited by twomers; 07-18-2006 at 01:32 PM.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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().

    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;
    Last edited by DougDbug; 07-18-2006 at 02:05 PM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>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
    Last edited by Ancient Dragon; 07-18-2006 at 02:47 PM.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    sprintf() is a standard C function and will do that. (Yet another reason for C superiority over C++ streams
    In base-36?????

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. populating a string only alpha characters
    By MSF1981 in forum C Programming
    Replies: 10
    Last Post: 02-06-2009, 10:58 AM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Replies: 23
    Last Post: 07-09-2007, 04:49 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Blocking Alpha characters
    By kimberly_weed@y in forum C Programming
    Replies: 3
    Last Post: 02-26-2005, 09:50 PM