C代码示例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
long estimate_file_size(const char* base64_data) {
long base64_length = strlen(base64_data);
long num_padding_chars = 0;
if (base64_data[base64_length - 1] == '=') {
num_padding_chars++;
if (base64_data[base64_length - 2] == '=') {
num_padding_chars++;
}
}
long estimated_file_size = num_base64_blocks * 3 / 4 - num_padding_chars;
return estimated_file_size;
}
int main() {
const char* base64_data = "VGhpcyBpcyBhIHRlc3Q=";
long size = estimate_file_size(base64_data);
printf("Estimated file size: %ld bytes\n", size);
return 0;
}
Python代码示例
import base64
import io
def calculate_image_size(base64_data):
image_data = base64.b64decode(base64_data)
data_length = len(image_data)
file_size = data_length / 1024
return file_size
base64_data = "iVBORw0KGg..."
file_size = calculate_image_size(base64_data)
print("文件大小:", file_size, "KB")