2024年C C++最全C语言base64编解码图片_c语言 图片 base64,2024年最新一次哔哩哔哩面试经历

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

unsigned int imageSize;        //图片字节数
char \*imageBin;               
char \*imageBase64;
char \*imageOutput;
size_t result;
char \*ret; 
unsigned int base64StrLength;

fp = fopen("test.jpg","rb");   //待编码图片
if (NULL == fp)
{
    printf("file open file");
	return -1;
}
//获取图片大小
fseek(fp, 0L, SEEK\_END);
imageSize = ftell(fp);
fseek(fp, 0L, SEEK\_SET);

//分配内存存储整个图片
imageBin = (char \*)malloc(sizeof(char)\*imageSize);
if (NULL == imageBin)
{
    printf("malloc failed");
 	return -1;
}

//读取图片
result = fread(imageBin, 1, imageSize, fp);
 if (result != imageSize)  
{  
    printf("file read failed");  
	return -1;
}  
fclose(fp);

//分配编码后图片所在buffer
imageBase64 = (char \*)malloc(sizeof(char)\*imageSize\*2);//因为编码一版会比源数据大1/3的样子,这里直接申请源文件一倍的空间
if (NULL == imageBase64)
{
    printf("malloc failed");
   	return -1;
}

//base64编码
base64\_encode(imageBin, imageBase64, imageSize);
base64StrLength = strlen(imageBase64);
printf("base64 str length:%d\n", base64StrLength);
printf("%s\n", imageBase64);

//分配存储解码数据buffer
imageOutput = (char \*)malloc(sizeof(char)\*imageSize);//解码后应该和源图片大小一致
if (NULL == imageBase64)
{
    printf("malloc failed");
   	return -1;
}
base64\_decode(imageBase64, imageOutput);

fp = fopen("output.jpg","wb");   
if (NULL == fp)
{
    printf("file open file");
	return -1;
}
fwrite(imageOutput, 1, imageSize, fp);
fclose(fp);

free(imageBin);
free(imageBase64);
free(imageOutput);

return 0;

}

const char * base64char = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/”;
char *base64_encode( const unsigned char * bindata, char * base64, int binlength )
{
int i, j;
unsigned char current;

for ( i = 0, j = 0 ; i < binlength ; i += 3 )
{
    current = (bindata[i] >> 2) ;
    current &= (unsigned char)0x3F;
    base64[j++] = base64char[(int)current];

    current = ( (unsigned char)(bindata[i] << 4 ) ) & ( (unsigned char)0x30 ) ;
    if ( i + 1 >= binlength )
    {
        base64[j++] = base64char[(int)current];
        base64[j++] = '=';
        base64[j++] = '=';
        break;
    }
    current |= ( (unsigned char)(bindata[i+1] >> 4) ) & ( (unsigned char) 0x0F );
    base64[j++] = base64char[(int)current];

    current = ( (unsigned char)(bindata[i+1] << 2) ) & ( (unsigned char)0x3C ) ;
    if ( i + 2 >= binlength )
    {
        base64[j++] = base64char[(int)current];
        base64[j++] = '=';
        break;
    }
    current |= ( (unsigned char)(bindata[i+2] >> 6) ) & ( (unsigned char) 0x03 );
    base64[j++] = base64char[(int)current];

    current = ( (unsigned char)bindata[i+2] ) & ( (unsigned char)0x3F ) ;
    base64[j++] = base64char[(int)current];
}
base64[j] = '\0';
return 0;

}

int base64_decode( const char * base64, unsigned char * bindata )
{
int i, j;
unsigned char k;
unsigned char temp[4];
for ( i = 0, j = 0; base64[i] != ‘\0’ ; i += 4 )
{
memset( temp, 0xFF, sizeof(temp) );
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i] )

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

https://bbs.csdn.net/topics/618668825)**

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值