Search code examples
iphoneobjective-ciosencodingbase32

Convert base 32 bit decoded string to decimal


I have one string which is base 32 bit decoded now I want to Decode that string I also want to encode any string to base 32 bit decoded string. Is there any way,any Algorithm(even in C) or any API for that so i can solve this issue. Thanx in advance.


Solution

  • I am not sure if I understand your question, but if you want to convert a base 32 number to base 10(decimal) number, take this:

    #include <stdio.h>                                                                                                                                          
    #include <string.h>
    #include <math.h>
    
    #define BASE 32
    
    unsigned int convert_number (const char *s) {
        unsigned int len = strlen(s) - 1;
        unsigned int result = 0;
        char start_ch = 0, ch;
        while(*s != '\0') {
            ch = *s;
            if (ch >= 'a') {
                start_ch = 'a' - 10;
            } else if (ch >= 'A') {
                start_ch = 'A' - 10;
            } else {
                start_ch = '0';
            }
    
            if(len >= 0)
                result += (ch - start_ch) * pow(BASE, len);
            else
                result += (ch - start_ch);
            ++s;
            --len;
        }
    
        return result;
    }