2012-05-08 42 views
0

我有一個字符串是基地32位解碼現在我想解碼該字符串我也想編碼任何字符串基地32位解碼字符串。 有沒有什麼辦法,任何算法(即使在C)或任何API,所以我可以解決這個問題。 Thanx提前。將基地32位解碼字符串轉換爲十進制

+0

可能重複[如何編碼的NSString與base32編碼?](http://stackoverflow.com/questions/5634759/how-to-encode-nsstring-with-base32-encoding) –

+0

什麼是*基地32位*? '32'表示用於編碼的字符數量,而不是一些位數。 – trojanfoe

+0

好的抱歉,但你知道任何算法有關嗎? –

回答

0

我不知道如果我理解你的問題,但如果你想要一個基地轉化32號到基數10(十進制)號碼,藉此:

#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; 
} 
+0

thanx很多Neevek,但我沒有得到你的代碼在這裏,如果ch = i,start_ch&result的值是什麼; –

+0

因爲''i'> ='a'','start_ch'將會是''''-10',所以'i'是十進制的'18',結果是18.您不需要知道start_ch的值是什麼,它用於將BASE-11和以上的數字轉換爲十進制數。 – neevek

相關問題