01编码

01编码

思路

转成字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
with open("./1.txt","r")as f2r:
contents = f2r.readlines()
string1=""
for content in contents:
content = content.replace("\n","")
print(content)
if len(content)==7:
string1+=content
elif len(content)==6:
content=content+"0"
string1+=content

print(string1)

反转

1755397831613-52cbc516-8a00-4009-992f-53d37620d15d.png

格雷码

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def decode_custom_gray(binary_chunks):
"""
将自定义格雷编码的二进制块解码为ASCII字符串

参数:
binary_chunks: 二进制字符串列表,每个字符串代表一个编码块

返回:
解码后的ASCII字符串
"""
# 定义格雷码到四进制的映射规则
GRAY_TO_BASE4 = {
'00': '0',
'01': '1',
'11': '2',
'10': '3'
}

result = []

for chunk in binary_chunks:
# 跳过非偶数长度的块
if len(chunk) % 2 != 0:
continue

try:
# 将二进制块分割为2位一组
pairs = [chunk[i:i + 2] for i in range(0, len(chunk), 2)]

# 转换为四进制数字
base4_digits = []
for pair in pairs:
if pair in GRAY_TO_BASE4:
base4_digits.append(GRAY_TO_BASE4[pair])
else:
raise ValueError(f"无效的二进制对: {pair}")

# 将四进制转换为十进制
decimal_value = int(''.join(base4_digits), 4)

# 只保留可打印ASCII字符(32-126)
if 32 <= decimal_value <= 126:
result.append(chr(decimal_value))
else:
result.append('?')

except (ValueError, KeyError):
result.append('?') # 无效的二进制对
except Exception:
result.append('?') # 其他转换错误

return ''.join(result)


def main():
# 测试数据
binary_chunks = [
'01010110', '01110101', '01111000', '01110010', '100000', '01111001', '100010', '01011010', '01010100', '100000', '01011010', '01111000', '100010', '01100111', '01110101', '100001', '01011010', '01100100', '01111100', '01100011', '100010', '01110101', '110001', '110001', '110001', '110001'
]

decoded = decode_custom_gray(binary_chunks)
print("解码结果:", decoded)


if __name__ == "__main__":
main()

转二维码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from PIL import Image
MAX = 600
#字符串长度
pic = Image.new("RGB",(MAX, MAX))
str = "数据省略"
i=0
for y in range (0,MAX):
for x in range (0,MAX):
if(str[i] == '1'):
pic.putpixel([x,y],(0, 0, 0))
else:
pic.putpixel([x,y],(255,255,255))
i = i+1
pic.show()
pic.save("flag.png")

例题

上海市赛(两位数)

更新: 2025-08-17 10:31:16
原文: https://www.yuque.com/chaye-apqbl/vsc85q/gwgpmhgagfbyy5g5


01编码
http://example.com/2025/01/19/MISC/编码/01编码/
Author
chaye
Posted on
January 19, 2025
Licensed under