base64解码
网络传输经常用base64编码的数据,因此我们需要将其解码成正常字符集合。
base64.h
#ifdef __cplusplus
extern "C" {
#endif char * base64_encode( const unsigned char * bindata, char * base64, int binlength );
int base64_decode( const char * base64, unsigned char * bindata ); #ifdef __cplusplus
}
#endif
base64.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "base64.h" #ifndef MAX_PATH
#define MAX_PATH 256
#endif const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; char * base64_encode( const unsigned char * bindata, char * base64, int binlength )
{
int i, j;
unsigned char current; for ( i = , j = ; i < binlength ; i += )
{
current = (bindata[i] >> ) ;
current &= (unsigned char)0x3F;
base64[j++] = base64char[(int)current]; current = ( (unsigned char)(bindata[i] << ) ) & ( (unsigned char)0x30 ) ;
if ( i + >= binlength )
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
base64[j++] = '=';
break;
}
current |= ( (unsigned char)(bindata[i+] >> ) ) & ( (unsigned char) 0x0F );
base64[j++] = base64char[(int)current]; current = ( (unsigned char)(bindata[i+] << ) ) & ( (unsigned char)0x3C ) ;
if ( i + >= binlength )
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
break;
}
current |= ( (unsigned char)(bindata[i+] >> ) ) & ( (unsigned char) 0x03 );
base64[j++] = base64char[(int)current]; current = ( (unsigned char)bindata[i+] ) & ( (unsigned char)0x3F ) ;
base64[j++] = base64char[(int)current];
}
base64[j] = '\0';
return base64;
} int base64_decode( const char * base64, unsigned char * bindata )
{
int i, j;
unsigned char k;
unsigned char temp[];
for ( i = , j = ; base64[i] != '\0' ; i += )
{
memset( temp, 0xFF, sizeof(temp) );
for ( k = ; k < ; k ++ )
{
if ( base64char[k] == base64[i] )
temp[]= k;
}
for ( k = ; k < ; k ++ )
{
if ( base64char[k] == base64[i+] )
temp[]= k;
}
for ( k = ; k < ; k ++ )
{
if ( base64char[k] == base64[i+] )
temp[]= k;
}
for ( k = ; k < ; k ++ )
{
if ( base64char[k] == base64[i+] )
temp[]= k;
} bindata[j++] = ((unsigned char)(((unsigned char)(temp[] << ))&0xFC)) |
((unsigned char)((unsigned char)(temp[]>>)&0x03));
if ( base64[i+] == '=' )
break; bindata[j++] = ((unsigned char)(((unsigned char)(temp[] << ))&0xF0)) |
((unsigned char)((unsigned char)(temp[]>>)&0x0F));
if ( base64[i+] == '=' )
break; bindata[j++] = ((unsigned char)(((unsigned char)(temp[] << ))&0xF0)) |
((unsigned char)(temp[]&0x3F));
}
return j;
}
base64解码的更多相关文章
- C# Base64解码 二进制输出
具体的:Base64编码解码还需要学习 /// <summary> /// base64 解码Excel下载 /// </summary> /// <param name ...
- 微信小程序之图片base64解码
不知道大家在做微信小程序的时候遇到base64解码的问题,我之前在做微信小程序的时候遇到base64解析图片一直有问题,所以在这里把遇到的问题和解决方案在这里记录一下: 在平时的项目中我们是直接用ba ...
- 将图片文件转化为字节数组字符串,并对其进行Base64编码处理,以及对字节数组字符串进行Base64解码并生成图片
实际开发中涉及图片上传并且量比较大的时候一般处理方式有三种 1.直接保存到项目中 最老土直接方法,也是最不适用的方法,量大对后期部署很不方便 2.直接保存到指定路径的服务器上.需要时候在获取,这种方式 ...
- SqlServer Base64解码中文
最近在做一个和拥有TurboCRM相关的项目,其中需要取出客户信息的联系人字段,经过查看,这个字段在存入时用Base64加密过了 这个功能在应用层实现是很方便的,但是由于一些特殊原因,只能放到SqlS ...
- python3 base64解码出现TypeError:Incorrect padding
今天在解决爬虫对加密参数的分析时,需要使用到base64解码.但是过程中出现了TypeError:Incorrect padding的错误提示.以下是解决方法,以便查阅. 其实正常使用base64是不 ...
- 【java工具类】对字节数组字符串进行Base64解码并生成图片
import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import org.springfra ...
- erlang的base64解码问题
在收到客户端的数字签名signature后,需要对signature做base64的解码.代码如下所示: validate(SignedRequest) -> RequestParts = st ...
- bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类 (二) 图片裁剪
图片裁剪参见: http://deepliquid.com/projects/Jcrop/demos.php?demo=thumbnail 一个js插件 http://www.mikes ...
- bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...
随机推荐
- DOM实战
作者声明:本博客中所写的文章,都是博主自学过程的笔记,参考了很多的学习资料,学习资料和笔记会注明出处,所有的内容都以交流学习为主.有不正确的地方,欢迎批评指正 视频来源:https://www.bil ...
- ffmpe安装
原文:https://www.jianshu.com/p/905df3d9e753 下载安装 下载最新源码包并解压 $ wget http://ffmpeg.org/releases/ffmpeg-3 ...
- Scrum立会报告+燃尽图(十一月二十一日总第二十九次):β阶段第二周分配任务
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...
- ext4.1入门
ExtJS简介 Ext是一个Ajax框架,用于在客户端创建丰富多彩的web应用程序界面,是在Yahoo!UI的基础上发展而来的.官方网址:www.sencha.com ExtJS是一个用来开发前端应用 ...
- Scrum Meeting Beta - 2
Scrum Meeting Beta - 2 NewTeam // 地点:新主楼F座二楼 任务反馈 团队成员 完成任务 计划任务 安万贺 了解缓存的相关内容Issue #109 设计本地存储的方案Is ...
- php对二维数组排序
function my_sort($arrays,$sort_key,$sort_order=SORT_DESC,$sort_type=SORT_NUMERIC ){ if(is_array($arr ...
- (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】
原文地址:http://www.cnblogs.com/melonblog/archive/2013/05/09/3062303.html 原文作者:豆浆油条 - melon 本文示例代码测试环境是W ...
- stm32f4xx系统总线架构
最近有人在STMCU社区网站咨询如下问题: 由于实验需要,要用到STM32F407的两个DMA并用定时器触发,在使用过程中发现DMA1无法把GPIO的IDR上的数据传输到内存,调试过程中出现DMA1的 ...
- javascript中对象和数组的异同点
一.JS声明对象或数组 JS对象:{ } JS数组:[ ] 对象 var b={m:'123',n:'abc'};alert(b.m);alert(b.n); 一维数组 var a=[1,2,3];a ...
- [HAOI2017]八纵八横 线性基
题面 题面 题解 观察到题目中的 "内陆经济环" 不好处理,因此我们把它拆成 "内陆经济链". 对于1号节点,我们创建一个它的复制节点n + 1号节点,这个节点 ...