[英]Base64 . I cant decode image files
我的代碼有問題。 我得到了bmp圖像並在base64中對其進行了編碼。然后我想對其進行解碼,但是我的PC上說:“無法加載圖像'OUTPUT.PNG'”。如何解決?
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
const unsigned char base64[] = {
// ASCII table
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
};
void deblock(const unsigned char *in, const unsigned char *base64, FILE * OutputFile, int len)
{
if (base64[(unsigned char)in[0]] < 64 && base64[(unsigned char)in[1]] < 64)
fprintf(OutputFile, "%c",(base64[(unsigned char)in[0]] << 2 | base64[(unsigned char)in[1]] >> 4));
if (base64[(unsigned char)in[1]] < 64 && base64[(unsigned char)in[2]] < 64)
fprintf(OutputFile, "%c",(base64[(unsigned char)in[1]] << 4 | base64[(unsigned char)in[2]] >> 2));
if (base64[(unsigned char)in[2]] < 64 && base64[(unsigned char)in[3]] < 64)
fprintf(OutputFile, "%c", base64[(unsigned char)in[2]] << 6 | base64[(unsigned char)in[3]]);
}
void decode (FILE *InputFile, FILE *OutputFile)
{
int len = 0;
unsigned char buffer[4] = "\0";
while ((len = fread(buffer,sizeof(unsigned char),4,InputFile)) > 0)
{
deblock(buffer,base64,OutputFile,len);
}
}
int check_decode(FILE *file1, FILE *file2)
{
int k = 0;
fseek (file1, 0, SEEK_END);
if (0 == ((ftell(file1)) % 4))
{
fseek(file1,0,SEEK_SET);
decode(file1,file2);
}
else
{
k = -1;
}
return k;
}
int main()
{
FILE *file1 = NULL;
FILE *file2 = NULL;
if (NULL == (file1 = fopen("INPUT1.txt", "rb")))
{
printf("Can't open INPUT file!");
return -1;
}
if (NULL == (file2 = fopen("OUTPUT.png", "wb ")))
{
printf("Can't create OUTPUT file");
fclose(file1);
return -1;
}
if ( -1 == check_decode(file1, file2) )
{
printf("Eto ne base64\n ");
return -1;
}
assert(file1);
assert(file2);
fclose(file1);
fclose(file2);
return 0;
}
我使用此網站對我的圖片進行編碼http://www.base64-image.de/step-2.php
我得到了bmp圖像並在base64中對其進行了編碼。然后我想對其進行解碼,但是我的PC上說:“無法加載圖像'OUTPUT.PNG'”。如何解決?
由於獲得了bmp圖像 ,因此只需創建OUTPUT.bmp
而不是OUTPUT.png
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.