base64编码C 代码

Base64是一种基于64个可打印字符来表示二进制数据的表示方法。由于 {\displaystyle 2^{6}=64} {\displaystyle 2^{6}=64},所以每6个比特为一个单元,对应某个可打印字符。3个字节有24个比特,对应于4个Base64单元,即3个字节可由4个可打印字符来表示。它可用来作为电子邮件的传输编码。在Base64中的可打印字符包括字母A-Z、a-z、数字0-9,这样共有62个字符,此外两个可打印符号在不同的系统中而不同。一些如uuencode的其他编码方法,和之后BinHex的版本使用不同的64字符集来代表6个二进制数字,但是不被称为Base64。
Base64编码要求把3个8位字节(38=24)转化为4个6位的字节(46=24),之后在6位的前面补两个0,形成8位一个字节的形式。 如果剩下的字符不足3个字节,则用0填充,输出字符使用‘=’,因此编码后输出的文本末尾可能会出现1或2个‘=’。

b64_encode: from VLC’s http.c

static char *base64_encode (char *out, int out_size, const unsigned char *in, int in_size)
{
    static const char b64[] =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    char *ret, *dst;
    unsigned i_bits = 0;
    int i_shift = 0;
    int bytes_remaining = in_size;

#define __UINT_MAX (~0lu)
#define __BASE64_SIZE(x)  (((x)+2) / 3 * 4 + 1)
#define __RB32(x)                                \
    (((uint32_t)((const uint8_t*)(x))[0] << 24) |    \
               (((const uint8_t*)(x))[1] << 16) |    \
               (((const uint8_t*)(x))[2] <<  8) |    \
                ((const uint8_t*)(x))[3])
    if (in_size >= __UINT_MAX / 4 ||
        out_size < __BASE64_SIZE(in_size))
        return NULL;
    ret = dst = out;
    while (bytes_remaining > 3) {
        i_bits = __RB32(in);
        in += 3; bytes_remaining -= 3;
        *dst++ = b64[ i_bits>>26        ];
        *dst++ = b64[(i_bits>>20) & 0x3F];
        *dst++ = b64[(i_bits>>14) & 0x3F];
        *dst++ = b64[(i_bits>>8 ) & 0x3F];
    }
    i_bits = 0;
    while (bytes_remaining) {
        i_bits = (i_bits << 8) + *in++;
        bytes_remaining--;
        i_shift += 8;
    }
    while (i_shift > 0) {
        *dst++ = b64[(i_bits << 6 >> i_shift) & 0x3f];
        i_shift -= 6;
    }
    while ((dst - ret) & 3)
        *dst++ = '=';
    *dst = '\0';

    return ret;
}

推荐一个在线转换工具:
[http://tool.oschina.net/encrypt?type=3]
更详细的代码请从gitHub上下载
[https://github.com/littlstar/b64.c]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言中,你可以使用以下代码来将JPEG图片进行Base64编码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/bio.h> #include <openssl/evp.h> char* base64_encode(const unsigned char* buffer, size_t length) { BIO *bio, *b64; FILE* stream; int encodedSize = 4 * ((length + 2) / 3); // 计算编码后的大小 char* base64 = (char*)malloc(encodedSize + 1); stream = fmemopen(base64, encodedSize + 1, "w"); b64 = BIO_new(BIO_f_base64()); bio = BIO_new_fp(stream, BIO_NOCLOSE); bio = BIO_push(b64, bio); BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); // 不添加换行符 BIO_write(bio, buffer, length); BIO_flush(bio); BIO_free_all(bio); fclose(stream); return base64; } int main() { FILE* file = fopen("image.jpg", "rb"); if (!file) { printf("无法打开图片文件\n"); return 1; } fseek(file, 0, SEEK_END); long fileSize = ftell(file); fseek(file, 0, SEEK_SET); unsigned char* buffer = (unsigned char*)malloc(fileSize); if (!buffer) { printf("内存分配失败\n"); fclose(file); return 1; } fread(buffer, fileSize, 1, file); fclose(file); char* base64 = base64_encode(buffer, fileSize); printf("%s\n", base64); free(buffer); free(base64); return 0; } ``` 这段代码使用了OpenSSL库中的BIO函数来进行Base64编码。你需要先安装OpenSSL库,然后使用gcc来编译上述代码: ``` gcc -o base64_encode base64_encode.c -lssl -lcrypto ``` 将图片文件命名为"image.jpg",然后运行编译后的可执行文件"base64_encode",即可得到JPEG图片的Base64编码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值