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”

全部评论

相关推荐

这个简历还有救吗,考研失利了,完蛋蛋了
helloWord大...:每次看见你们9爷隔着嚷嚷找不到工作,我真的分不清是串还是装
点赞 评论 收藏
分享
哞客37422655...:你猜为什么福利这么好还得一直追着你问
点赞 评论 收藏
分享
KKorz:是这样的,还会定期默写抽查
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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