python2.7实现16进制转二进制数据

搜索ffd8ffdb找到了一个百度知道上的问题。

问题:悬赏1000分,谁能把这写变成图片?

题主给出的16进制字符串如下

FFD8FFDB004300080404040404080404040808080808100C080808081410100C101814181818141818181C2420181C241C1818202C2024282828282818202C302C283024282828FFDB004301080808080808140C0C14281C181C2828282828282828282828282828282828282828282828282828282828282828282828282828282828282828282828282828FFC000110800A0008003012200021101031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FAFFC4001F0100030101010101010101010000000000000102030405060708090A0BFFC400B51100020102040403040705040400010277000102031104052131061241510761711322328108144291A1B1C109233352F0156272D10A162434E125F11718191A262728292A35363738393A434445464748494A535455565758595A636465666768696A737475767778797A82838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE2E3E4E5E6E7E8E9EAF2F3F4F5F6F7F8F9FAFFDA000C03010002110311003F00F2E0C4700D28DABC9FC714C2720823A77A148C104E6AB631DC93383F2E79A039C7CC298AF8E08FA52E79E94B416A39793CD3871CE7AF614D4009A72E3D3F5A7718B83EBD7A73401C700FBD00647038FAD3D57D074A3415C41B54E7D6946D39C8A509F3F029C1179C67DE815F51A235604951C502085BEF2D48A841C763E94E11617069683D4805B42C795148DA6DB3E72A3EB563CB1D40A0AE0F1DE9DB415D951B49B53FF2CC1F4C534E856D82C062AE902940A560E6926652BF078C7BD2F19EB4C534EC1FBC7A5031C0E3B7E34F1C8CD35416193534103C9C2A1C9F414C048D0EEF9454890B3F406B5F48F095EDF1C98C807BE2BA8D27C076F1E0DC28240F4A2F626F6389B7D2AE6603CB8989FA56959F84B53B95C880D7A05AE85A7D9A80235E2A733D8DB8DBB871DA92BB1731C2C5E01D458E59715623F87774472DF5AEB64D6ACE3FBBF98A84F886DC1E10FE54F9593CC7347E1E5CE301F9A826F00EA51AFC8338AEB17C496C3EF237D6A58F5FD3E4E1CE3EB4F958F9CF3FBBF0D6A769CBDB923D40AA525BB47F2BA107DC57A9A4BA7DE0E0AB0354753F0A69BA80256201BD40A5A8F9AE79B18CA9E714D24FD2B775DF0ADE6965A440590720FA5633A1190451B8FA186148EF522026A353CF3C55BD32D1AEE711AAF534B62C9B4DD36E2F2409126726BB4F0E7836284092E1727AF353F863C3B0DA4219E3E71D715B335DC76ABB17938A12B9939327B6B6B5B24C0518A7CB72767EE307D2B3AE2E1E78082DB7DE974926252649B23DEAAC41388AFAE4E49C0A923D191B995C9A95750B60768957E953A5C44FF75C1A2ED0D10268F68B8CA669E34CB407FD483E9C557BED696CA5DA4F5AB76D7497118955A86DD810C3A55A30C794BF95452E856520CF978FA0AB9BBDE92470A85BDA926D0EC635D787248BE7B3988C76CD4106B5A86993082FD495E9BAAD1F1091766D4AE79F4A65F15D414C6F1738E0E2AD79936340FD9756B5C8018115C378B3423A75C192352158F15BBA05DCD67766CA46F973C55BF16

ffd8ffdb开头的是jpg格式,用python转一下就出来了,由于数据量小,这里直接用binascii这个库转。有兴趣的可以尝试用struct

python2.7的代码如下:

# -*- coding: UTF-8 -*-
import binascii
photohexstr = 'FFD8FFDB004300080404040404080404040808080808100C080808081410100C101814181818141818181C2420181C241C1818202C2024282828282818202C302C283024282828FFDB004301080808080808140C0C14281C181C2828282828282828282828282828282828282828282828282828282828282828282828282828282828282828282828282828FFC000110800A0008003012200021101031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FAFFC4001F0100030101010101010101010000000000000102030405060708090A0BFFC400B51100020102040403040705040400010277000102031104052131061241510761711322328108144291A1B1C109233352F0156272D10A162434E125F11718191A262728292A35363738393A434445464748494A535455565758595A636465666768696A737475767778797A82838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE2E3E4E5E6E7E8E9EAF2F3F4F5F6F7F8F9FAFFDA000C03010002110311003F00F2E0C4700D28DABC9FC714C2720823A77A148C104E6AB631DC93383F2E79A039C7CC298AF8E08FA52E79E94B416A39793CD3871CE7AF614D4009A72E3D3F5A7718B83EBD7A73401C700FBD00647038FAD3D57D074A3415C41B54E7D6946D39C8A509F3F029C1179C67DE815F51A235604951C502085BEF2D48A841C763E94E11617069683D4805B42C795148DA6DB3E72A3EB563CB1D40A0AE0F1DE9DB415D951B49B53FF2CC1F4C534E856D82C062AE902940A560E6926652BF078C7BD2F19EB4C534EC1FBC7A5031C0E3B7E34F1C8CD35416193534103C9C2A1C9F414C048D0EEF9454890B3F406B5F48F095EDF1C98C807BE2BA8D27C076F1E0DC28240F4A2F626F6389B7D2AE6603CB8989FA56959F84B53B95C880D7A05AE85A7D9A80235E2A733D8DB8DBB871DA92BB1731C2C5E01D458E59715623F87774472DF5AEB64D6ACE3FBBF98A84F886DC1E10FE54F9593CC7347E1E5CE301F9A826F00EA51AFC8338AEB17C496C3EF237D6A58F5FD3E4E1CE3EB4F958F9CF3FBBF0D6A769CBDB923D40AA525BB47F2BA107DC57A9A4BA7DE0E0AB0354753F0A69BA80256201BD40A5A8F9AE79B18CA9E714D24FD2B775DF0ADE6965A440590720FA5633A1190451B8FA186148EF522026A353CF3C55BD32D1AEE711AAF534B62C9B4DD36E2F2409126726BB4F0E7836284092E1727AF353F863C3B0DA4219E3E71D715B335DC76ABB17938A12B9939327B6B6B5B24C0518A7CB72767EE307D2B3AE2E1E78082DB7DE974926252649B23DEAAC41388AFAE4E49C0A923D191B995C9A95750B60768957E953A5C44FF75C1A2ED0D10268F68B8CA669E34CB407FD483E9C557BED696CA5DA4F5AB76D7497118955A86DD810C3A55A30C794BF95452E856520CF978FA0AB9BBDE92470A85BDA926D0EC635D787248BE7B3988C76CD4106B5A86993082FD495E9BAAD1F1091766D4AE79F4A65F15D414C6F1738E0E2AD79936340FD9756B5C8018115C378B3423A75C192352158F15BBA05DCD67766CA46F973C55BF16'
if len(photohexstr) % 2 != 0:
    photohexstr = photohexstr + '0'
content = binascii.a2b_hex(photohexstr)
fmt = binascii.b2a_hex(content[0:4]) # 读取前4字节转化为16进制字符串
print fmt
phototype = {'47494638': '.gif', 'ffd8ffe0': '.jpg', 'ffd8ffe1': '.jpg', 'ffd8ffdb': '.jpg', '89504e47': '.png'}  # 智能识别图片文件格式               
qualified_file_name =  'testphoto' + phototype[fmt]    
open(qualified_file_name, 'wb').write(content) 
print u'转换完成'
转换完成之后是一张破损的图片

用看图王打开是这样的

python2.7实现16进制转二进制数据_第1张图片

发这篇博客的时候发现CSDN博客好像升级图片的博客地址了,变成了http://img.blog.csdn.net/+时间的格式

你可能感兴趣的:(python,二进制,转换,16进制)