C中的Base64到Base10解码

时间:2015-03-26 11:25:43

标签: c base64 base

我是编程的新手,我正在尝试将十进制数转换为base64并再次返回。我的代码的编码部分工作,但我不能让解码部分工作。任何人都可以帮忙吗?

/*
 ============================================================================
 Name        : base64.c
 Author      : Peter Doyle
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>

static char *base64enc(long unsigned int value)
{
    char base64[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    /* log(2**64) / log(64) = 12.38 => max 13 char + '\0' */
    char buffer[12];
    unsigned int offset = sizeof(buffer);

    buffer[--offset] = '\0';
    do {
        buffer[--offset] = base36[value % 64];
    } while (value /= 64);

    return strdup(&buffer[offset]);
}


int base2base(unsigned char in[], int basein, int baseout)
{
    int J;
    int K;
    int DecimalValue;

    int InputNumberLength;

    //unsigned char OutputValue[];

    unsigned char NumericBaseData[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"};

    //convert input number to base 10.
    InputNumberLength = sizeof(in);
    DecimalValue = 0;
    for (J = 0; J < InputNumberLength; J++){
        for (K = 0; K < basein; K++){
            char sub1;
            char sub2;
            char inJ = (in+J);
            char NumericBaseDataK = (NumericBaseData+K);
            strncpy(sub1, inJ, 1);
            strncpy(sub2, NumericBaseDataK, 1);
                if (sub1 == sub2){
                    int Calc = ((K-1)*(pow(basein,(InputNumberLength-J)))+.5);
                    DecimalValue = DecimalValue+Calc;
                }
        }
    }
    return DecimalValue;
}

int main(void) {
    long unsigned int test = 6000;
    char *encoded = base36enc(test);
    puts(encoded);
    base2base(encoded, 64, 10);
    printf("%d\n", DecimalValue);
    return EXIT_SUCCESS;

2 个答案:

答案 0 :(得分:0)

如果str是你在base 64中有数字的字符串,只需执行以下操作将其转换为base 10:

int function(static char *str)
{
int i = 0;
int decimal = 0;
int mult = 1;
while (str[i])
   i++;
i--;
while (i >= 0)
   {
       decimal += mult * get_index(str[i]);
       i--;
       mult = mult * 64;
   }
return decimal;
}

其中get_index获取NumericBaseData中char str [i]的索引 Tbh我没有测试它,但应该工作

编辑: get_index就像这样

int get_index(char c)
{
    char base64[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    int i = 0;
    while (c != base64[i])
         i++;
    return i;

}

如果您使用大数字,可能会将类型更改为long long,因为base 64中的smth可以非常快地实现

答案 1 :(得分:0)

我发现@dietbacon的答案对于解码旧项目中的数据库很有用。因此,我修改了解决方案以利用C ++ -std = c ++ 17。 B64编码非常适合将数字压缩到文件中,例如长度,索引等。

static const std::string base64_chars = 
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // 26
             "abcdefghijklmnopqrstuvwxyz" // 26
             "0123456789+/"; // 12

int base64_decode_number(const std::string& input) {
    // Decode a positive number. Return 0 When the input contains a not valid character or overflow will ocurr;

    const int N64 = 64;
    const int overflow_LIMIT = 5; // Prevent N64*N64*N64... > std::numeric_limits<int>::max();

    if ( input.empty() || input.size() > overflow_LIMIT ) return  0;
    int pos_val = 1; // positional value
    int result = 0;
    for( auto itpos = input.rbegin(); itpos < input.rend(); itpos++){

        if ( size_t digit_b10 = base64_chars.find(*itpos); 
                    digit_b10 == base64_chars.npos ) return 0;
        else {
            result += pos_val*static_cast<int>(digit_b10);
            pos_val *= N64;
        }
    }
    return result;
}