python 中 w 和 wb 的区别

import struct

def ch(num):
    if (num >= '0' and num <= '9'):
        return ord(num) - ord('0')
    elif (num >= 'A' and num <= 'F'):
        return ord(num) - ord('A') + 10
    else:
        return ord(num) - ord('a') + 10

def Hex2Str(text):
    s = ""
    for i in range(0, len(text), 2):
        print (text[i], text[i+1])
        x = ch(text[i]) * 16 + ch(text[i + 1])
        print (x)
        try:
            s += struct.pack('b', x)
        except:
            s += struct.pack('B', x)
    print (s)
    return s

Str = Hex2Str("000A0B0C0D0E0F112233445566778899AABBCCDDEEFF")
print (type(Str))
f = open("struct.bin", "w")
f.write(Str)
f.close()
f = open("struct2.bin", "wb")
f.write(Str)
f.close()

图片说明
图片说明
注意到上面的write格式是“w”,0A在输出时会格式化成0D0A
所以想要编辑bin文件要原样照应时,需要用“wb”

全部评论

相关推荐

用微笑面对困难:985只有在应届生里面的优势是断层的在社招或者更远的工作中算是后续能力优先级
工作压力大,你会干什么?
点赞 评论 收藏
分享
01-19 15:14
已编辑
延安大学 C++
累死的一条狗:我说白了这种玩意你直接点举报就完事了在给他挂出来
找工作以来,你最看不惯_...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务