【问题标题】:Base64 to Base10 decoding in CC语言中的Base64到Base10解码
【发布时间】:2015-03-26 11:25:42
【问题描述】:

我是编程新手,正在尝试将十进制数转换为 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;

【问题讨论】:

  • 您遇到什么错误?
  • 你是在base 36还是base 64编码?请选择一个。
  • 我想这可能就是你要找的东西:stackoverflow.com/a/9197570/4707373 ?
  • @MalteR 我认为 OP 只对适合的情况感兴趣。
  • ^ 是 XOR 运算符。

标签: c base64 base


【解决方案1】:

如果 str 是您拥有以 64 为基数的数字的字符串,只需执行以下操作将其转换为以 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 可以很快变得非常大

【讨论】:

    【解决方案2】:

    我发现@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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      相关资源
      最近更新 更多