惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Stack Overflow Blog
Stack Overflow Blog
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
L
LangChain Blog
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
Y
Y Combinator Blog
月光博客
月光博客
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Fortinet All Blogs
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
小众软件
小众软件
H
Help Net Security
Last Week in AI
Last Week in AI
B
Blog RSS Feed
宝玉的分享
宝玉的分享
N
Netflix TechBlog - Medium
博客园 - 叶小钗
The GitHub Blog
The GitHub Blog

Bing's Blog

终不似,少年游 我现在难以输出一篇完整的文章 git-cm:在终端里用 LLM 生成 commit message Token Tracker - 追踪 Coding Agent 的 Token 使用情况 AI Coding 工具的实践经验 2025年终总结 行车记录仪视频拼接 生成文章摘要 DMA拼接 2024年终总结 二维数据可视化工具-painter 对ToB和ToC的感受 C++闭包二 轻量级参数解析库-tiny_cmdline C++标签派发技术
Base64压缩
bing · 2025-04-17 · via Bing's Blog

首先, 明确base64并不是一个压缩算法. 但是在某些场景我们可以使用其编码特性达到压缩的效果.

如下demo展示的一种应用场景:

 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
26
27
28
29
import base64
import json

origin_data = {
    "config1": 1,
    "config2": 2,
    "calibration_list": [
        0.465485,
        0.693291,
        0.914242,
        0.438485,
        0.621781,
        0.148442,
        0.536921,
        0.789463,
        0.069955,
        0.649050,
    ]
}

print("before compress", len(json.dumps(origin_data)))

compress_data = origin_data
# contact calibration_list as a bytes string and then base64 encode
calibration_bytes = b"".join(
    [bytes([int(x * 255)]) for x in origin_data["calibration_list"]]
)
compress_data["calibration_list"] = base64.b64encode(calibration_bytes).decode("utf-8")
print("after compress", len(json.dumps(compress_data)))

输出:

1
2
before compress 149
after compress 68

config1config2一般是需要人工调整的配置, 所以需要明文表示. calibration_list一般由机器生成和阅读, 所以不太关注其可读性.

这两种配置又期望用同一个配置文件管理, 那么通过将calibration_list转换为二进制数据, 然后base64编码, 这样既使得配置文件可以以文本方式解析, 又相对减少了文件大小, 在一些空间/传输速率敏感的场合适用.

以上demo只是一种应用场景, 细节需要具体问题具体分析.

base64编码后的大小约等于原大小的4/3(增大了空间), 压缩比例和base64编码的对象+精度需求相关. 以json格式+7位精度需求的32bit浮点数组为例, 一个数字表示至少需要8B(1B逗号+7B数字), 但是base64编码后只需要6B. 这种情况下压缩率75%.