Unknown pdf encoding from JSON response - python

I have an API that returns a pdf from json, but it just returns as a long string of integers like following
[{"status":"SUCCESS"},{"data":"37,80,68,70,45,49,46,52,10,37,-45,-21,-23,-31,10,49,32,48,32,111,98,106,10,60,60,47,84,105,116,108,101,32,40,49,49,32,67,83,45,73,73,32,32,83,117,98,106,101,99,116,105,118,101,32,81,46,...
...,1,32,49,55,10,47,82,111,111,116,32,56,32,48,32,82,10,47,73,110,102,111,32,49,32,48,32,82,62,62,10,115,116,97,114,116,120,114,101,102,10,54,55,54,56,53,10,37,37,69,79,70"}
My questions are:
What is this encoding?
How to convert this into a pdf using python?
P.S: Here is the endpoint to get the full response.

The beginning of data is a hint that you actually have a list of the bytes values of the PDF file: it starts with the byte values of '%PDF-1.4'.
So you must first extract that curious string:
data = json_data[1]['data']
to have:
"37,80,68,70,45,49,46,52,10,37,-45,-21,-23,-31,10,49,32,48,32,111,98,106,10,60,60,47,84,105,116,108,101,32,40,49,49,32,67,83,45,73,73,32,32,83,117,98,106,101,99,116,105,118,101,32,81,46, ..."
convert it to a list of int first, then a byte string (i if i >=0 else i+256 ensure positive values...):
intlist = [int(i) for i in data.split(",")]
b = bytes(i if i >=0 else i+256 for i in intlist)
to get b'%PDF-1.4\n%\xd3\xeb\xe9\xe1\n1 0 obj\n<</Title (11 CS-II Subjective Q...'
And finaly save that to a file:
with open('file.pdf', 'wb') as fd:
fd.write(b)

Related

Hex String to Image File from varbinary(max)

I have a table in a database which stores image files in varbinary(max) type. I would like to extract, convert and save the image file. Then, I used the cast as varcharmax to extract:
cast([IMG_FILE] as varchar(max))
The result of this cast looks like a hex string (I've removed part of string to protect the privacy of the person):
\xffd8ffe000104a46494600010100000100010000fffe003b43524541544f523a2067642d6a7065672076312e3020287573696e6720494a47204a50454720763632292c207175616c697479203d2037350affdb004300080606070605080707070909080a0c140d0c0b0b0c1912130f141d1a1f1e1d1a1c1c20242e2720222c231c1c2837292c30313434341f27393d38323c2e333432ffdb0043010909090c0b0c180d0d1832211c213232323232323232323232323232323232323232323232323232323232323232323232323232323232323232323232323232ffc000110801e0018003012200021101031101ffc4001f0000010501010101010100000000000000000102030405060708090a0bffc400b5100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9faffc4001f0100030101010101010101010000000000000102030405060708090a0bffc400b51100020102040403040705040400010277000102031104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda000c03010002110311003f00f7676c606d18c0ea334ddf8ecbff007cd39f185e7f845338f5cd4bb8c5de40fbaa3fe034d2e78e17fef914eebd3a5340cd1d01313cc3d36aff00df229371ecabff007c8a5231d2914366a6ed0d88243fdd5ffbe452ef20636a7fdf228c61b19a1ba8a1b121379c1ca271fec8a6f9a48e113fef914ee71d6902e471473581ec024207089ff7c8a42e7b2aff00df229083d05041f5a24f99d90262190a8fba9ff7c8a6f9849190bff7c8a561d39cd23039cd2d7a0085f031b53fef914d121e8427fdf229db78dd9a611cd2d52d07a5c56705c602ff00df2290ccd9c6d4ff00be4518fce9a179343040643d36affdf229a642171b53fef914bef4d61934936210c8c31f2a7fdf229a643fdd5ffbe45291c719a0a9229df5b8fd06994f4da9ff007c8a634ac38dabff007c8a52bd293193d687a8ae233f1d171fee8a42e71d17fef914e6038c1cd308c8a86f41a1bbdb00617fef914d676c636aff00df229d8f7a61269a62b06ef970557fef9151ef6033b57fef914f24e0d37195c114ef71dc6bb96c7cabff007c8a432118185ffbe4538819f6a6e016a96f501a58e7eeaffdf229a5db7745ff00be4539b03a1a615ee0d1cce3a0c6ef2011b57fef914d0c7d17fef9141fad2018f5a96f415c52e76e36affdf229be6606085ffbe452b76a8ce0e0f38a2d7455f5144879e17fef9151b16c7dd5ff00be4548704719151382070c69abf526fa15dc9cfdd5ff00be4522363b2ffdf2295f205469cd26c49dc9fcc6c636afb7ca283237a2ff00df22902e7192683c526afb957d86b139e8bff7c8a371e0e17fef9a70e4649a61f41569f98842fcf45ffbe453d5b7c4f9000c0edef4c200e94e8c651f24f4feb431791e8cc01c67fba29360fc2a43dbe94801c62ba04479c0c0a403d0d49b71de82075c54bd1e8047b493d38a0549819c83499ed498c888e71da9b8e7bd4d8079a4e33d68695f4023dbc74a4c10b8c91529eb8ce69a49c63934ec85a918000e4d379ed521507bd2600e452d2d6434bb8c2a7bd21008e49a93934d2bef495c08f0318e69a4735285c0e4d371f5a56761dc6601ee69303d4d3c820f34d2bde86b5b8911ed1d3b5260f41526063bd23151f74934f60647d38a6924f7a70cd0400696c088b83de902d48401ce6a3660bd5b152ec03700714de808aad2dec68df7c71eb5564d6add7ab8cfb5351e657173a5a17cae7b9a36918f4ace4d66d9cffac03eb436b100e932fe747237b20724b7341867a66a307b66a9c7abc0cdb44c326a65ba46e323eb4493ec11922561900534a91eb8a72c8a69c5b90012452bad8a202a0fd28381c0cd48cbe9d29a411d6a795010b2827ad0063d85388f9860d29c639a1bf20b11300718a6e0edc53c8cf20d3704f4a4af14090cdb918cd46e3039353e2a365c8a399ee162948d938a8d783562551ce0557546dd424ac277e8591961405e2841814fc6053e55d4688f0aa29ac722a4007f11e69a403d2a7975125d488e3a629ea0847c7a0fe74a149e7a53c708ff004feb54a36d4773d0db181f4a074eb4a7b7d290574589131ef46294d203c53b2189ed40e99a70028153cb740348a6ed1ef4fe28346880600077a4c11df8a7e28a16c0c8f68c505401c53f6f7cf148dd695900cda09e4d211da9fb79e4d21e33536ea806119a691934fcf3487079a76ee172365c91cd211818e952718a630cf7a997604308c0c03c5340515260014d2bc668f31a23200a85e403a9a49ee1501f9b8ae6b58f11dad846c4c9f30a23173d8994d47566bdcde2c6092c063d6b95d5fc5f05aa952ca71ef5c0f883e20e4be1be5ed835e6ba9789aeaf9d88f941f7aeba54a0a3766337397c27a36b9e3d241f29f1f8d7232f8c6e8b191676c7a66b93dcf28def2135195ddc838acf9aced1d098d1b3bc99d4ff00c26b7f21ff005ac07d6abcde30bfdf949dc7a8cd736a769c1a693939c50a524ef73a9f2f2d91d647e33d45006331c7d6ba2d1fe225ce4099d881d6bcc2aca4e513e5f94d5aa925e6651a315767d13a5f8cadaee252b2e4f7aeaec7515b98c152bcfbd7cad61ab5c5a4c1a3948f519eb5dbe87f1027b670928e3d41a9f62aa2e65a14ef17eeea7d04a463ef0a4233d49ae0b44f18c37e5433ed26bb3b6bd8e75051b7715cd38d8719296c4c579143026a45f9f9348eac0641e2a514c8f6fa7029a40031ba9f83c734854679a4eedea043823a1a69e739a94e33c1a615fad16b0fc8a9228a8d5483c1a9a5003707351e08a495d581d895464629714a99f5a083db9a6d7612b11b73d79a4cf1c715260e6908e714d30b0c1d39a545051c7b0fe74ec63922950e56407d3fad55fa01e80c3a7d293da949e9f4a4e86ba0421f4a31ef4b4829083a714734b4668630c51d28a0000d0f4013149c8a773499a5600c66931834bda8a9d980d2bcd210318079a791e949b78e4d0047b78eb49803a53f6e3bd348ed52d8c65260639a7851de98dd7da9598ba8c6031ed54e7ba11a9cb60517b7ab164ee000af35f1978a1adcb2452ede3b1ada9d273d889cf976347c4be26b7b457559b0476af14f13f8a26ba91d6397afa1aced6b5fb8bbb8649a46e7eed614a8001963b8f352deb62e8c1cfdf90af70f71018db9e725aaa84cf19a452c0100d27dd6cf5a6958d5b5bd8937793f2900d46f206ed8a473939a6d34ba99b7d828141a314c91c8477141618c629a0e0d14157d0703ed4e572ad9076fd2a3a2815d9b1a6eb773672ae18919af5ef09f8ba2b88d10cbf37a5785e48e86af699a94da75d2ca8c719e4538422fdd9688c9c1a7cd13eb0b4bd49e31b5b922ade094af29f09f8ba3bb31aac849e339af5082713a2b29e08ac2709537666b1929ec4b8ed9a4239c669e17d4d042f1827359b5a94884a1ce3148460e2a43b9bbe714c6f9693f20b6a539940622a2079c5589706a118f4fc695da0f21e9c7069eb919c522e0e3e6cd4b803a1a7b088f1914c0073cf22a5c0cf34dc00738a1ad6e0b61873b7ad2c60857ebd07f3a5dbd48a55dc11ce7b0fe7556ea80ef5bb7d29bdb14e6edf4a4ae9243b6281c503ad0681876c51db1477a3bd0213be28f6a5a08a000fa5276a5a3bd2b0076a4c714a7eb476a76b0c4f5148697bd045660371410734b9e682734b96c0467e5fa551bbb95894f356e66db19e7a5717e25d51a0b497071c75a50d7414df2ab9cd78d7c5f1d8a3471b027d735e23acf88a4d46566790e074029fe2dd5e4bbbd740f900fad73182a37706bae724a3c9131a51937cecb25bcf21dcfe350ce36b0c396146c326020fcaa55b296460a4115cd751ea77c94a5d0a59a39abeda5caa326a06b4901c053f88a6aa45eccc7d9c8af454be4b02734d319155742e576b8ca4a52a451834c9128a28a005a01a28c7340077a706229761a530b0ed4ae8a519762ee93aa4fa65da4b1390b9f987ad7bcf84bc4d1dfc119593823a1af9e30548ae87c37aecda7dea0f30aa67a55b9732b313f755cfa962963950609a918818c571fe1bd716ea2521f7715d746448809ae59e9a3634efaa0c0e7b5464362a52bef4846062b392b2d0ab3dd95258cf7aae0f241e055bb824f7aa6793823f1a6924ee4dee4a80638a93b535146060d498c1da47e349eaf41b637b734cf518a93be0d34a91c814db698ad713660511802393e83f9d29fad3907cae4fa0fe745f41eccee58703e9494a7b7d290f15d64074a3a504d1de801314bda8a0f4a0618e28a3b518a00314514502000628ed8a5c521a560128a5e31450d0c4c519e0d29e941e10e6a5ad00ccd4a4f2e1246315e39f10b5ef22d6684100b29e6bd33c4774d0c2c41e315f3af8e6fcdddcb82c0807079a984b91f31369549282382690ccecce4e7b1a96d2d1e7942004e7d7a540ff7f6818adfd1d0aa82cd914abd4718b91d14e9ddf2a5b1a5a7e86a8a3705e7a915a83478970579ab369e4ac39cfd6a413a76af0aa55ab2773b13e5d119d269e8010462b367b34f73f856ccf3ef6212a8cae429c8c0aba72982bee667d8e0f2cef8c7b5567b18d81250afd2b48b231e7a52336480bfad752a9240a16462bd826dc8e9555ecdb38515bb28557dbc54261dc8f81c8e95b46b48538dd6a60b5b32f0460d362b7dcc41ad10e76ec75c9f5a6e571854c9f5ae8f68f617b185d3b6840205098da2905ba93f2e39ab009c95dbf8d34a283d714b999a4a104d6831ad9d0659463da9de5ee5ce0f4a91d8ecfbd9a4492458b8a57614d3bbe633a48ca37351862a4107906af5c4259039618aa2c306ba212ba386b53e591e9fe00d564dca8ec79af6eb09fcd814af422be78f025c289b637506bdf7459035aa63d2b9eaa49e88c29c3951b5b7e514c2083d6a4ec29a473cd4a7a17b15a6191554801aaeca7e5354c8c9a6a4d6c0da1e83d2a6078a8d3afb54c38031d296ec345a91f6e39a42c777b548700f069b81914df611195a910028f9f41fce9a7ae29c87f76ff41fcea40ed8f403da93bd2b76fa52576884c76a53d28a3b5020c7140eb8a0d140c38a3bf147d6814083b52e78c5277a281876a4141345020c52f6c514679a0031da9929db19a79351ceb9898fa54caf60381f165c1585f7600c57cd9e2194b6a3721c900b715f4278cc3341213d81af9d3c452017854f27359a4ddac441be666282cec3daba2d2c648504f22b0202049c9c0ae97492b8c1fc2b2c53b44edc3adce8208418301c83de9aca51701b8a9229422e579f5aaf24aace49c827a578cb99b3a9a4d5c8598231c3735565bb6fbbb43525c3fcdc1aafbf6c6db860f635d5082dd9297ba364726e1146429a9d8a6e0031355a396e0e3f761aa54573212ca466b592b0e1252f7424453306627e9eb434721ced6280f6a74aac17383ed512a4b2725cfd285b5ee5db5d0a8ec1414da091dea247081976edcd5d36c77161d7bd40f6eccd8ad949131e5bd9958c9c1403f1a74414fdf19a9becf818c73eb519421f6e0d55d3d8bbab0d7556caa80bef51a02b959188fa55a11c663225e0f6a3ecb1ba125d81edc53524b7314efa14278c795f23122a8739c56c4b6fb21c2f35972aed278eb5b5295cc2bd3b2b9d1f8324db7d8248e6be85f0ecb9b7418ea2be6ff0b4c22d4d3271935f4678625530a1073c56357994fc8c15ba6e74e14e3a527be69fe664f14c391ef46b6021917208aaa54038356e56238aaac79eb4d790ada8a9c0c54aa78a621ce2a618c74a76e50d861a41d6a461f2f0298471cf152ddec3e846d9dd8a7a0da8ff0041fce838cd3971b1fe83f9d0c0ecdba0fa537da95bb7d28aec2043e94bda93bd2f7a061da8068ef4628101e4d1d28a281873d293da97a0a2810518e29334b40c00a4a7629a68105046518514a7ee9a996c079ef8ce3416f2640e86be63f1044dfda92c871b43702bea1f18dbef864e07435f3278ad366aac07af22953dac42b29ea6270f90a315ada4b3f98a8188ac78ced6cf6addd3514ca8c8c49c56588d22ceea366d3674510645209e290c0eec5bb76ab36f1798b935656061f285fcebc3752cce8e5d6e658b3129da78a73d80520655beb566e008589dea3f1aa12dca93c30fceb48b94b54529ae83bcb58895db83ed51b3739e955e4bbf9b99302a27bb4f3026e24915b2a7264a945487cd2bb0c7040a8fcc60463bd33ce0723207d0d264e460d6aa36d0b7364f23055f94f5eb9a58e48c00acb93ea6a84d2beff5aad2ddc8770dd8c7a55aa4da26d6dcb525c33ccea170a0f14d424b162df8566c770fbf96a735f1070056dec9ad110a50b6a5d2a6460ac6ac44932f2d82a2b23edac1f76734f8f5173260b10b4dd295b412ab052d4d6914b9e00c5646a518598053c62b421bc8647f2c125aa0be8f70391cd4d3bc656644a6a7068afa2e7fb46300f39afa23c228e6de3f98f4af01f0e5b193548dc7406be8cf0a478b643ed57566ef6471c2cd9d5247f2f5a08c53c6303146077a84b42caf20c8aa6f9cd5e71551d7069e8c96220e2a75c91c542a08a9d3914d6d60419e714c7cf4a93b7bfad34824d36b4d02c46579a78188dfe83f9d04fad2a9f91f1e83f9d24ac0762ddbe9494addbe9499cd758838a28ed450018a31c51477a04262968cf345001451463a503d8314518e6949a042514678a2801314bc631452123073d693d86723e2a8c7d9e42403c57cc1e33876eaccc38eb915f4ef8ae5548189c1e2be66f1a9f335a241c0c1e2a694ad744b8373491cac285e60a3b9aec74db5558d5516b98d3a267b8f947435ddd846228836706b831f55a563be853f76e5b863f2bef702ab5f6aa911d8b264e39aafa8ea4b121cb57197d7524f312ae42d71e1f0aeabe691556ab5a245cbfd5da595c019c1f5acc7bd95fa315fa55720f3934daf6a34a114924717349160dc398f0cc4d2a5c396fbc4d56f6a729c1aae54109b4d1a30b54a6f593208cfa5518dc8e8695d98724e7358b826f53bb9fddba2d4973e6444631ef59e5cf3cd4dbf2318c540fc135708a5a184db4b723cf34679a292b539ae3b3401c8f4a3a1a9c3a607cb49bb1508a96ecb510810060cf9f6157e3413a6e2485c77aa10ba87520e2b5e30b2c58ef5c955d8e9850e57a3d0b1e188a31aae37743dabdf7c3cc896ea0376af9a4dcc9a65de6372ac79c8af4ff03789a5982ac921661c735a28f36fff000e73b5
I tried to used this hex string in a online tool (https://codepen.io/abdhass/full/jdRNdj), and the image is corrected displayed (remembering that I've cutted part of string to preserve the persons privacy):
Then, I've tried to take this hex string and tried to convert to a image file using python3. I've been trying a lot of things (the majority found here), but until now, I coudn't save the correct file.
Saving directly doesn't generate the image.
with open(photo_path + 'file.jpg', 'wb') as new_jpg:
new_jpg.write(hexString)
Using binascii.unhexlify returns "Non-hexadecimal digit found"
binascii.unhexlify(hexString)
Converting to int/bin returns invalid literal for int() with base 16:
bin(int(hexString, 16))[2:]
I would like to know how to solve this problem? That is, I would like to take this hex string and save a image file in my computer.
If I have string without \x then I can convert every two chars to integer value, create bytearray and save it
text = 'ffd8ffe000104a46494600010100000100010000fffe003b43524541544f523a2067642d6a7065672076312e3020287573696e6720494a47204a50454720763632292c207175616c697479203d2037350affdb004300080606070605080707070909080a0c140d0c0b0b0c1912130f141d1a1f1e1d1a1c1c20242e2720222c231c1c2837292c30313434341f27393d38323c2e333432ffdb0043010909090c0b0c180d0d1832211c213232323232323232323232323232323232323232323232323232323232323232323232323232323232323232323232323232ffc000110801e0018003012200021101031101ffc4001f0000010501010101010100000000000000000102030405060708090a0bffc400b5100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9faffc4001f0100030101010101010101010000000000000102030405060708090a0bffc400b51100020102040403040705040400010277000102031104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda000c03010002110311003f00f7676c606d18c0ea334ddf8ecbff007cd39f185e7f845338f5cd4bb8c5de40fbaa3fe034d2e78e17fef914eebd3a5340cd1d01313cc3d36aff00df229371ecabff007c8a5231d2914366a6ed0d88243fdd5ffbe452ef20636a7fdf228c61b19a1ba8a1b121379c1ca271fec8a6f9a48e113fef914ee71d6902e471473581ec024207089ff7c8a42e7b2aff00df229083d05041f5a24f99d90262190a8fba9ff7c8a6f9849190bff7c8a561d39cd23039cd2d7a0085f031b53fef914d121e8427fdf229db78dd9a611cd2d52d07a5c56705c602ff00df2290ccd9c6d4ff00be4518fce9a179343040643d36affdf229a642171b53fef914bef4d61934936210c8c31f2a7fdf229a643fdd5ffbe45291c719a0a9229df5b8fd06994f4da9ff007c8a634ac38dabff007c8a52bd293193d687a8ae233f1d171fee8a42e71d17fef914e6038c1cd308c8a86f41a1bbdb00617fef914d676c636aff00df229d8f7a61269a62b06ef970557fef9151ef6033b57fef914f24e0d37195c114ef71dc6bb96c7cabff007c8a432118185ffbe4538819f6a6e016a96f501a58e7eeaffdf229a5db7745ff00be4539b03a1a615ee0d1cce3a0c6ef2011b57fef914d0c7d17fef9141fad2018f5a96f415c52e76e36affdf229be6606085ffbe452b76a8ce0e0f38a2d7455f5144879e17fef9151b16c7dd5ff00be4548704719151382070c69abf526fa15dc9cfdd5ff00be4522363b2ffdf2295f205469cd26c49dc9fcc6c636afb7ca283237a2ff00df22902e7192683c526afb957d86b139e8bff7c8a371e0e17fef9a70e4649a61f41569f98842fcf45ffbe453d5b7c4f9000c0edef4c200e94e8c651f24f4feb431791e8cc01c67fba29360fc2a43dbe94801c62ba04479c0c0a403d0d49b71de82075c54bd1e8047b493d38a0549819c83499ed498c888e71da9b8e7bd4d8079a4e33d68695f4023dbc74a4c10b8c91529eb8ce69a49c63934ec85a918000e4d379ed521507bd2600e452d2d6434bb8c2a7bd21008e49a93934d2bef495c08f0318e69a4735285c0e4d371f5a56761dc6601ee69303d4d3c820f34d2bde86b5b8911ed1d3b5260f41526063bd23151f74934f60647d38a6924f7a70cd0400696c088b83de902d48401ce6a3660bd5b152ec03700714de808aad2dec68df7c71eb5564d6add7ab8cfb5351e657173a5a17cae7b9a36918f4ace4d66d9cffac03eb436b100e932fe747237b20724b7341867a66a307b66a9c7abc0cdb44c326a65ba46e323eb4493ec11922561900534a91eb8a72c8a69c5b90012452bad8a202a0fd28381c0cd48cbe9d29a411d6a795010b2827ad0063d85388f9860d29c639a1bf20b11300718a6e0edc53c8cf20d3704f4a4af14090cdb918cd46e3039353e2a365c8a399ee162948d938a8d783562551ce0557546dd424ac277e8591961405e2841814fc6053e55d4688f0aa29ac722a4007f11e69a403d2a7975125d488e3a629ea0847c7a0fe74a149e7a53c708ff004feb54a36d4773d0db181f4a074eb4a7b7d290574589131ef46294d203c53b2189ed40e99a70028153cb740348a6ed1ef4fe28346880600077a4c11df8a7e28a16c0c8f68c505401c53f6f7cf148dd695900cda09e4d211da9fb79e4d21e33536ea806119a691934fcf3487079a76ee172365c91cd211818e952718a630cf7a997604308c0c03c5340515260014d2bc668f31a23200a85e403a9a49ee1501f9b8ae6b58f11dad846c4c9f30a23173d8994d47566bdcde2c6092c063d6b95d5fc5f05aa952ca71ef5c0f883e20e4be1be5ed835e6ba9789aeaf9d88f941f7aeba54a0a3766337397c27a36b9e3d241f29f1f8d7232f8c6e8b191676c7a66b93dcf28def2135195ddc838acf9aced1d098d1b3bc99d4ff00c26b7f21ff005ac07d6abcde30bfdf949dc7a8cd736a769c1a693939c50a524ef73a9f2f2d91d647e33d45006331c7d6ba2d1fe225ce4099d881d6bcc2aca4e513e5f94d5aa925e6651a315767d13a5f8cadaee252b2e4f7aeaec7515b98c152bcfbd7cad61ab5c5a4c1a3948f519eb5dbe87f1027b670928e3d41a9f62aa2e65a14ef17eeea7d04a463ef0a4233d49ae0b44f18c37e5433ed26bb3b6bd8e75051b7715cd38d8719296c4c579143026a45f9f9348eac0641e2a514c8f6fa7029a40031ba9f83c734854679a4eedea043823a1a69e739a94e33c1a615fad16b0fc8a9228a8d5483c1a9a5003707351e08a495d581d895464629714a99f5a083db9a6d7612b11b73d79a4cf1c715260e6908e714d30b0c1d39a545051c7b0fe74ec63922950e56407d3fad55fa01e80c3a7d293da949e9f4a4e86ba0421f4a31ef4b4829083a714734b4668630c51d28a0000d0f4013149c8a773499a5600c66931834bda8a9d980d2bcd210318079a791e949b78e4d0047b78eb49803a53f6e3bd348ed52d8c65260639a7851de98dd7da9598ba8c6031ed54e7ba11a9cb60517b7ab164ee000af35f1978a1adcb2452ede3b1ada9d273d889cf976347c4be26b7b457559b0476af14f13f8a26ba91d6397afa1aced6b5fb8bbb8649a46e7eed614a8001963b8f352deb62e8c1cfdf90af70f71018db9e725aaa84cf19a452c0100d27dd6cf5a6958d5b5bd8937793f2900d46f206ed8a473939a6d34ba99b7d828141a314c91c8477141618c629a0e0d14157d0703ed4e572ad9076fd2a3a2815d9b1a6eb773672ae18919af5ef09f8ba2b88d10cbf37a5785e48e86af699a94da75d2ca8c719e4538422fdd9688c9c1a7cd13eb0b4bd49e31b5b922ade094af29f09f8ba3bb31aac849e339af5082713a2b29e08ac2709537666b1929ec4b8ed9a4239c669e17d4d042f1827359b5a94884a1ce3148460e2a43b9bbe714c6f9693f20b6a539940622a2079c5589706a118f4fc695da0f21e9c7069eb919c522e0e3e6cd4b803a1a7b088f1914c0073cf22a5c0cf34dc00738a1ad6e0b61873b7ad2c60857ebd07f3a5dbd48a55dc11ce7b0fe7556ea80ef5bb7d29bdb14e6edf4a4ae9243b6281c503ad0681876c51db1477a3bd0213be28f6a5a08a000fa5276a5a3bd2b0076a4c714a7eb476a76b0c4f5148697bd045660371410734b9e682734b96c0467e5fa551bbb95894f356e66db19e7a5717e25d51a0b497071c75a50d7414df2ab9cd78d7c5f1d8a3471b027d735e23acf88a4d46566790e074029fe2dd5e4bbbd740f900fad73182a37706bae724a3c9131a51937cecb25bcf21dcfe350ce36b0c396146c326020fcaa55b296460a4115cd751ea77c94a5d0a59a39abeda5caa326a06b4901c053f88a6aa45eccc7d9c8af454be4b02734d319155742e576b8ca4a52a451834c9128a28a005a01a28c7340077a706229761a530b0ed4ae8a519762ee93aa4fa65da4b1390b9f987ad7bcf84bc4d1dfc119593823a1af9e30548ae87c37aecda7dea0f30aa67a55b9732b313f755cfa962963950609a918818c571fe1bd716ea2521f7715d746448809ae59e9a3634efaa0c0e7b5464362a52bef4846062b392b2d0ab3dd95258cf7aae0f241e055bb824f7aa6793823f1a6924ee4dee4a80638a93b535146060d498c1da47e349eaf41b637b734cf518a93be0d34a91c814db698ad713660511802393e83f9d29fad3907cae4fa0fe745f41eccee58703e9494a7b7d290f15d64074a3a504d1de801314bda8a0f4a0618e28a3b518a00314514502000628ed8a5c521a560128a5e31450d0c4c519e0d29e941e10e6a5ad00ccd4a4f2e1246315e39f10b5ef22d6684100b29e6bd33c4774d0c2c41e315f3af8e6fcdddcb82c0807079a984b91f31369549282382690ccecce4e7b1a96d2d1e7942004e7d7a540ff7f6818adfd1d0aa82cd914abd4718b91d14e9ddf2a5b1a5a7e86a8a3705e7a915a83478970579ab369e4ac39cfd6a413a76af0aa55ab2773b13e5d119d269e8010462b367b34f73f856ccf3ef6212a8cae429c8c0aba72982bee667d8e0f2cef8c7b5567b18d81250afd2b48b231e7a52336480bfad752a9240a16462bd826dc8e9555ecdb38515bb28557dbc54261dc8f81c8e95b46b48538dd6a60b5b32f0460d362b7dcc41ad10e76ec75c9f5a6e571854c9f5ae8f68f617b185d3b6840205098da2905ba93f2e39ab009c95dbf8d34a283d714b999a4a104d6831ad9d0659463da9de5ee5ce0f4a91d8ecfbd9a4492458b8a57614d3bbe633a48ca37351862a4107906af5c4259039618aa2c306ba212ba386b53e591e9fe00d564dca8ec79af6eb09fcd814af422be78f025c289b637506bdf7459035aa63d2b9eaa49e88c29c3951b5b7e514c2083d6a4ec29a473cd4a7a17b15a6191554801aaeca7e5354c8c9a6a4d6c0da1e83d2a6078a8d3afb54c38031d296ec345a91f6e39a42c777b548700f069b81914df611195a910028f9f41fce9a7ae29c87f76ff41fcea40ed8f403da93bd2b76fa52576884c76a53d28a3b5020c7140eb8a0d140c38a3bf147d6814083b52e78c5277a281876a4141345020c52f6c514679a0031da9929db19a79351ceb9898fa54caf60381f165c1585f7600c57cd9e2194b6a3721c900b715f4278cc3341213d81af9d3c452017854f27359a4ddac441be666282cec3daba2d2c648504f22b0202049c9c0ae97492b8c1fc2b2c53b44edc3adce8208418301c83de9aca51701b8a9229422e579f5aaf24aace49c827a578cb99b3a9a4d5c8598231c3735565bb6fbbb43525c3fcdc1aafbf6c6db860f635d5082dd9297ba364726e1146429a9d8a6e0031355a396e0e3f761aa54573212ca466b592b0e1252f7424453306627e9eb434721ced6280f6a74aac17383ed512a4b2725cfd285b5ee5db5d0a8ec1414da091dea247081976edcd5d36c77161d7bd40f6eccd8ad949131e5bd9958c9c1403f1a74414fdf19a9becf818c73eb519421f6e0d55d3d8bbab0d7556caa80bef51a02b959188fa55a11c663225e0f6a3ecb1ba125d81edc53524b7314efa14278c795f23122a8739c56c4b6fb21c2f35972aed278eb5b5295cc2bd3b2b9d1f8324db7d8248e6be85f0ecb9b7418ea2be6ff0b4c22d4d3271935f4678625530a1073c56357994fc8c15ba6e74e14e3a527be69fe664f14c391ef46b6021917208aaa54038356e56238aaac79eb4d790ada8a9c0c54aa78a621ce2a618c74a76e50d861a41d6a461f2f0298471cf152ddec3e846d9dd8a7a0da8ff0041fce838cd3971b1fe83f9d0c0ecdba0fa537da95bb7d28aec2043e94bda93bd2f7a061da8068ef4628101e4d1d28a281873d293da97a0a2810518e29334b40c00a4a7629a68105046518514a7ee9a996c079ef8ce3416f2640e86be63f1044dfda92c871b43702bea1f18dbef864e07435f3278ad366aac07af22953dac42b29ea6270f90a315ada4b3f98a8188ac78ced6cf6addd3514ca8c8c49c56588d22ceea366d3674510645209e290c0eec5bb76ab36f1798b935656061f285fcebc3752cce8e5d6e658b3129da78a73d80520655beb566e008589dea3f1aa12dca93c30fceb48b94b54529ae83bcb58895db83ed51b3739e955e4bbf9b99302a27bb4f3026e24915b2a7264a945487cd2bb0c7040a8fcc60463bd33ce0723207d0d264e460d6aa36d0b7364f23055f94f5eb9a58e48c00acb93ea6a84d2beff5aad2ddc8770dd8c7a55aa4da26d6dcb525c33ccea170a0f14d424b162df8566c770fbf96a735f1070056dec9ad110a50b6a5d2a6460ac6ac44932f2d82a2b23edac1f76734f8f5173260b10b4dd295b412ab052d4d6914b9e00c5646a518598053c62b421bc8647f2c125aa0be8f70391cd4d3bc656644a6a7068afa2e7fb46300f39afa23c228e6de3f98f4af01f0e5b193548dc7406be8cf0a478b643ed57566ef6471c2cd9d5247f2f5a08c53c6303146077a84b42caf20c8aa6f9cd5e71551d7069e8c96220e2a75c91c542a08a9d3914d6d60419e714c7cf4a93b7bfad34824d36b4d02c46579a78188dfe83f9d04fad2a9f91f1e83f9d24ac0762ddbe9494addbe9499cd758838a28ed450018a31c51477a04262968cf345001451463a503d8314518e6949a042514678a2801314bc631452123073d693d86723e2a8c7d9e42403c57cc1e33876eaccc38eb915f4ef8ae5548189c1e2be66f1a9f335a241c0c1e2a694ad744b8373491cac285e60a3b9aec74db5558d5516b98d3a267b8f947435ddd846228836706b831f55a563be853f76e5b863f2bef702ab5f6aa911d8b264e39aafa8ea4b121cb57197d7524f312ae42d71e1f0aeabe691556ab5a245cbfd5da595c019c1f5acc7bd95fa315fa55720f3934daf6a34a114924717349160dc398f0cc4d2a5c396fbc4d56f6a729c1aae54109b4d1a30b54a6f593208cfa5518dc8e8695d98724e7358b826f53bb9fddba2d4973e6444631ef59e5cf3cd4dbf2318c540fc135708a5a184db4b723cf34679a292b539ae3b3401c8f4a3a1a9c3a607cb49bb1508a96ecb510810060cf9f6157e3413a6e2485c77aa10ba87520e2b5e30b2c58ef5c955d8e9850e57a3d0b1e188a31aae37743dabdf7c3cc896ea0376af9a4dcc9a65de6372ac79c8af4ff03789a5982ac921661c735a28f36fff000e73b5'
integers = []
while text:
value = int(text[:2], 16)
integers.append(value)
text = text[2:]
data = bytearray(integers)
with open('output.jpg', 'wb') as fh:
print(fh.write(data))
If I have string with \x then first \xff is treated as char's code so I have to use ord() to convert it integer.
text = '\xffd8ffe000104a46494600010100000100010000fffe003b43524541544f523a2067642d6a7065672076312e3020287573696e6720494a47204a50454720763632292c207175616c697479203d2037350affdb004300080606070605080707070909080a0c140d0c0b0b0c1912130f141d1a1f1e1d1a1c1c20242e2720222c231c1c2837292c30313434341f27393d38323c2e333432ffdb0043010909090c0b0c180d0d1832211c213232323232323232323232323232323232323232323232323232323232323232323232323232323232323232323232323232ffc000110801e0018003012200021101031101ffc4001f0000010501010101010100000000000000000102030405060708090a0bffc400b5100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9faffc4001f0100030101010101010101010000000000000102030405060708090a0bffc400b51100020102040403040705040400010277000102031104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda000c03010002110311003f00f7676c606d18c0ea334ddf8ecbff007cd39f185e7f845338f5cd4bb8c5de40fbaa3fe034d2e78e17fef914eebd3a5340cd1d01313cc3d36aff00df229371ecabff007c8a5231d2914366a6ed0d88243fdd5ffbe452ef20636a7fdf228c61b19a1ba8a1b121379c1ca271fec8a6f9a48e113fef914ee71d6902e471473581ec024207089ff7c8a42e7b2aff00df229083d05041f5a24f99d90262190a8fba9ff7c8a6f9849190bff7c8a561d39cd23039cd2d7a0085f031b53fef914d121e8427fdf229db78dd9a611cd2d52d07a5c56705c602ff00df2290ccd9c6d4ff00be4518fce9a179343040643d36affdf229a642171b53fef914bef4d61934936210c8c31f2a7fdf229a643fdd5ffbe45291c719a0a9229df5b8fd06994f4da9ff007c8a634ac38dabff007c8a52bd293193d687a8ae233f1d171fee8a42e71d17fef914e6038c1cd308c8a86f41a1bbdb00617fef914d676c636aff00df229d8f7a61269a62b06ef970557fef9151ef6033b57fef914f24e0d37195c114ef71dc6bb96c7cabff007c8a432118185ffbe4538819f6a6e016a96f501a58e7eeaffdf229a5db7745ff00be4539b03a1a615ee0d1cce3a0c6ef2011b57fef914d0c7d17fef9141fad2018f5a96f415c52e76e36affdf229be6606085ffbe452b76a8ce0e0f38a2d7455f5144879e17fef9151b16c7dd5ff00be4548704719151382070c69abf526fa15dc9cfdd5ff00be4522363b2ffdf2295f205469cd26c49dc9fcc6c636afb7ca283237a2ff00df22902e7192683c526afb957d86b139e8bff7c8a371e0e17fef9a70e4649a61f41569f98842fcf45ffbe453d5b7c4f9000c0edef4c200e94e8c651f24f4feb431791e8cc01c67fba29360fc2a43dbe94801c62ba04479c0c0a403d0d49b71de82075c54bd1e8047b493d38a0549819c83499ed498c888e71da9b8e7bd4d8079a4e33d68695f4023dbc74a4c10b8c91529eb8ce69a49c63934ec85a918000e4d379ed521507bd2600e452d2d6434bb8c2a7bd21008e49a93934d2bef495c08f0318e69a4735285c0e4d371f5a56761dc6601ee69303d4d3c820f34d2bde86b5b8911ed1d3b5260f41526063bd23151f74934f60647d38a6924f7a70cd0400696c088b83de902d48401ce6a3660bd5b152ec03700714de808aad2dec68df7c71eb5564d6add7ab8cfb5351e657173a5a17cae7b9a36918f4ace4d66d9cffac03eb436b100e932fe747237b20724b7341867a66a307b66a9c7abc0cdb44c326a65ba46e323eb4493ec11922561900534a91eb8a72c8a69c5b90012452bad8a202a0fd28381c0cd48cbe9d29a411d6a795010b2827ad0063d85388f9860d29c639a1bf20b11300718a6e0edc53c8cf20d3704f4a4af14090cdb918cd46e3039353e2a365c8a399ee162948d938a8d783562551ce0557546dd424ac277e8591961405e2841814fc6053e55d4688f0aa29ac722a4007f11e69a403d2a7975125d488e3a629ea0847c7a0fe74a149e7a53c708ff004feb54a36d4773d0db181f4a074eb4a7b7d290574589131ef46294d203c53b2189ed40e99a70028153cb740348a6ed1ef4fe28346880600077a4c11df8a7e28a16c0c8f68c505401c53f6f7cf148dd695900cda09e4d211da9fb79e4d21e33536ea806119a691934fcf3487079a76ee172365c91cd211818e952718a630cf7a997604308c0c03c5340515260014d2bc668f31a23200a85e403a9a49ee1501f9b8ae6b58f11dad846c4c9f30a23173d8994d47566bdcde2c6092c063d6b95d5fc5f05aa952ca71ef5c0f883e20e4be1be5ed835e6ba9789aeaf9d88f941f7aeba54a0a3766337397c27a36b9e3d241f29f1f8d7232f8c6e8b191676c7a66b93dcf28def2135195ddc838acf9aced1d098d1b3bc99d4ff00c26b7f21ff005ac07d6abcde30bfdf949dc7a8cd736a769c1a693939c50a524ef73a9f2f2d91d647e33d45006331c7d6ba2d1fe225ce4099d881d6bcc2aca4e513e5f94d5aa925e6651a315767d13a5f8cadaee252b2e4f7aeaec7515b98c152bcfbd7cad61ab5c5a4c1a3948f519eb5dbe87f1027b670928e3d41a9f62aa2e65a14ef17eeea7d04a463ef0a4233d49ae0b44f18c37e5433ed26bb3b6bd8e75051b7715cd38d8719296c4c579143026a45f9f9348eac0641e2a514c8f6fa7029a40031ba9f83c734854679a4eedea043823a1a69e739a94e33c1a615fad16b0fc8a9228a8d5483c1a9a5003707351e08a495d581d895464629714a99f5a083db9a6d7612b11b73d79a4cf1c715260e6908e714d30b0c1d39a545051c7b0fe74ec63922950e56407d3fad55fa01e80c3a7d293da949e9f4a4e86ba0421f4a31ef4b4829083a714734b4668630c51d28a0000d0f4013149c8a773499a5600c66931834bda8a9d980d2bcd210318079a791e949b78e4d0047b78eb49803a53f6e3bd348ed52d8c65260639a7851de98dd7da9598ba8c6031ed54e7ba11a9cb60517b7ab164ee000af35f1978a1adcb2452ede3b1ada9d273d889cf976347c4be26b7b457559b0476af14f13f8a26ba91d6397afa1aced6b5fb8bbb8649a46e7eed614a8001963b8f352deb62e8c1cfdf90af70f71018db9e725aaa84cf19a452c0100d27dd6cf5a6958d5b5bd8937793f2900d46f206ed8a473939a6d34ba99b7d828141a314c91c8477141618c629a0e0d14157d0703ed4e572ad9076fd2a3a2815d9b1a6eb773672ae18919af5ef09f8ba2b88d10cbf37a5785e48e86af699a94da75d2ca8c719e4538422fdd9688c9c1a7cd13eb0b4bd49e31b5b922ade094af29f09f8ba3bb31aac849e339af5082713a2b29e08ac2709537666b1929ec4b8ed9a4239c669e17d4d042f1827359b5a94884a1ce3148460e2a43b9bbe714c6f9693f20b6a539940622a2079c5589706a118f4fc695da0f21e9c7069eb919c522e0e3e6cd4b803a1a7b088f1914c0073cf22a5c0cf34dc00738a1ad6e0b61873b7ad2c60857ebd07f3a5dbd48a55dc11ce7b0fe7556ea80ef5bb7d29bdb14e6edf4a4ae9243b6281c503ad0681876c51db1477a3bd0213be28f6a5a08a000fa5276a5a3bd2b0076a4c714a7eb476a76b0c4f5148697bd045660371410734b9e682734b96c0467e5fa551bbb95894f356e66db19e7a5717e25d51a0b497071c75a50d7414df2ab9cd78d7c5f1d8a3471b027d735e23acf88a4d46566790e074029fe2dd5e4bbbd740f900fad73182a37706bae724a3c9131a51937cecb25bcf21dcfe350ce36b0c396146c326020fcaa55b296460a4115cd751ea77c94a5d0a59a39abeda5caa326a06b4901c053f88a6aa45eccc7d9c8af454be4b02734d319155742e576b8ca4a52a451834c9128a28a005a01a28c7340077a706229761a530b0ed4ae8a519762ee93aa4fa65da4b1390b9f987ad7bcf84bc4d1dfc119593823a1af9e30548ae87c37aecda7dea0f30aa67a55b9732b313f755cfa962963950609a918818c571fe1bd716ea2521f7715d746448809ae59e9a3634efaa0c0e7b5464362a52bef4846062b392b2d0ab3dd95258cf7aae0f241e055bb824f7aa6793823f1a6924ee4dee4a80638a93b535146060d498c1da47e349eaf41b637b734cf518a93be0d34a91c814db698ad713660511802393e83f9d29fad3907cae4fa0fe745f41eccee58703e9494a7b7d290f15d64074a3a504d1de801314bda8a0f4a0618e28a3b518a00314514502000628ed8a5c521a560128a5e31450d0c4c519e0d29e941e10e6a5ad00ccd4a4f2e1246315e39f10b5ef22d6684100b29e6bd33c4774d0c2c41e315f3af8e6fcdddcb82c0807079a984b91f31369549282382690ccecce4e7b1a96d2d1e7942004e7d7a540ff7f6818adfd1d0aa82cd914abd4718b91d14e9ddf2a5b1a5a7e86a8a3705e7a915a83478970579ab369e4ac39cfd6a413a76af0aa55ab2773b13e5d119d269e8010462b367b34f73f856ccf3ef6212a8cae429c8c0aba72982bee667d8e0f2cef8c7b5567b18d81250afd2b48b231e7a52336480bfad752a9240a16462bd826dc8e9555ecdb38515bb28557dbc54261dc8f81c8e95b46b48538dd6a60b5b32f0460d362b7dcc41ad10e76ec75c9f5a6e571854c9f5ae8f68f617b185d3b6840205098da2905ba93f2e39ab009c95dbf8d34a283d714b999a4a104d6831ad9d0659463da9de5ee5ce0f4a91d8ecfbd9a4492458b8a57614d3bbe633a48ca37351862a4107906af5c4259039618aa2c306ba212ba386b53e591e9fe00d564dca8ec79af6eb09fcd814af422be78f025c289b637506bdf7459035aa63d2b9eaa49e88c29c3951b5b7e514c2083d6a4ec29a473cd4a7a17b15a6191554801aaeca7e5354c8c9a6a4d6c0da1e83d2a6078a8d3afb54c38031d296ec345a91f6e39a42c777b548700f069b81914df611195a910028f9f41fce9a7ae29c87f76ff41fcea40ed8f403da93bd2b76fa52576884c76a53d28a3b5020c7140eb8a0d140c38a3bf147d6814083b52e78c5277a281876a4141345020c52f6c514679a0031da9929db19a79351ceb9898fa54caf60381f165c1585f7600c57cd9e2194b6a3721c900b715f4278cc3341213d81af9d3c452017854f27359a4ddac441be666282cec3daba2d2c648504f22b0202049c9c0ae97492b8c1fc2b2c53b44edc3adce8208418301c83de9aca51701b8a9229422e579f5aaf24aace49c827a578cb99b3a9a4d5c8598231c3735565bb6fbbb43525c3fcdc1aafbf6c6db860f635d5082dd9297ba364726e1146429a9d8a6e0031355a396e0e3f761aa54573212ca466b592b0e1252f7424453306627e9eb434721ced6280f6a74aac17383ed512a4b2725cfd285b5ee5db5d0a8ec1414da091dea247081976edcd5d36c77161d7bd40f6eccd8ad949131e5bd9958c9c1403f1a74414fdf19a9becf818c73eb519421f6e0d55d3d8bbab0d7556caa80bef51a02b959188fa55a11c663225e0f6a3ecb1ba125d81edc53524b7314efa14278c795f23122a8739c56c4b6fb21c2f35972aed278eb5b5295cc2bd3b2b9d1f8324db7d8248e6be85f0ecb9b7418ea2be6ff0b4c22d4d3271935f4678625530a1073c56357994fc8c15ba6e74e14e3a527be69fe664f14c391ef46b6021917208aaa54038356e56238aaac79eb4d790ada8a9c0c54aa78a621ce2a618c74a76e50d861a41d6a461f2f0298471cf152ddec3e846d9dd8a7a0da8ff0041fce838cd3971b1fe83f9d0c0ecdba0fa537da95bb7d28aec2043e94bda93bd2f7a061da8068ef4628101e4d1d28a281873d293da97a0a2810518e29334b40c00a4a7629a68105046518514a7ee9a996c079ef8ce3416f2640e86be63f1044dfda92c871b43702bea1f18dbef864e07435f3278ad366aac07af22953dac42b29ea6270f90a315ada4b3f98a8188ac78ced6cf6addd3514ca8c8c49c56588d22ceea366d3674510645209e290c0eec5bb76ab36f1798b935656061f285fcebc3752cce8e5d6e658b3129da78a73d80520655beb566e008589dea3f1aa12dca93c30fceb48b94b54529ae83bcb58895db83ed51b3739e955e4bbf9b99302a27bb4f3026e24915b2a7264a945487cd2bb0c7040a8fcc60463bd33ce0723207d0d264e460d6aa36d0b7364f23055f94f5eb9a58e48c00acb93ea6a84d2beff5aad2ddc8770dd8c7a55aa4da26d6dcb525c33ccea170a0f14d424b162df8566c770fbf96a735f1070056dec9ad110a50b6a5d2a6460ac6ac44932f2d82a2b23edac1f76734f8f5173260b10b4dd295b412ab052d4d6914b9e00c5646a518598053c62b421bc8647f2c125'
integers = []
value = ord(text[0])
integers.append(value)
text = text[1:]
while text:
value = int(text[:2], 16)
integers.append(value)
text = text[2:]
data = bytearray(integers)
with open('output.jpg', 'wb') as fh:
print(fh.write(data))
Because string in your question is incomplete so it creates incomplete image.
But with data from link it create correct JPG file.
EDIT:
It seems you have raw string and \x is treated as normal string, not part of byte \xff - and you have to remove \x at start using using text = text[2:]
text = r'\xffd8ff...'
integers = []
text = text[2:]
while text:
value = int(text[:2], 16)
integers.append(value)
text = text[2:]
data = bytearray(integers)
with open('output.jpg', 'wb') as fh:
print(fh.write(data))
EDIT:
Simpler version with standard module codecs. It still need to remove \x from string.
If you have bytes:
text = b'\\xffd8ff...' # bytes
import codecs
text = text[2:] # remove `\x`
data = codecs.decode(text, 'hex_codec')
with open('output.jpg', 'wb') as fh:
fh.write(data)
If you have string - then you have to first encode() to bytes:
text = '\\xffd8ff...' # string
import codecs
text = text.encode() # bytes
text = text[2:] # remove `\x`
data = codecs.decode(text, 'hex_codec')
with open('output-1.jpg', 'wb') as fh:
fh.write(data)

Read binary file and check with matching character in python

I would like to scan through data files from GPS receiver byte-wise (actually it will be a continuous flow, not want to test the code with offline data). If find a match, then check the next 2 bytes for the 'length' and get the next 2 bytes and shift 2 bits(not byte) to the right, etc. I didn't handle binary before, so stuck in a simple task. I could read the binary file byte-by-byte, but can not find a way to match by desired pattern (i.e. D3).
with open("COM6_200417.ubx", "rb") as f:
byte = f.read(1) # read 1-byte at a time
while byte != b"":
# Do stuff with byte.
byte = f.read(1)
print(byte)
The output file is:
b'\x82'
b'\xc2'
b'\xe3'
b'\xb8'
b'\xe0'
b'\x00'
b'#'
b'\x13'
b'\x05'
b'!'
b'\xd3'
b'\x00'
b'\x13'
....
how to check if that byte is == '\xd3'? (D3)
also would like to know how to shift bit-wise, as I need to check decimal value consisting of 6 bits
(1-byte and next byte's first 2-bits). Considering, taking 2-bytes(8-bits) and then 2-bit right-shift
to get 6-bits. Is it possible in python? Any improvement/addition/changes are very much appreciated.
ps. can I get rid of that pesky 'b' from the front? but if ignoring it does not affect then no problem though.
Thanks in advance.
'That byte' is represented with a b'' in front, indicating that it is a byte object. To get rid of it, you can convert it to an int:
thatbyte = b'\xd3'
byteint = thatbyte[0] # or
int.from_bytes(thatbyte, 'big') # 'big' or 'little' endian, which results in the same when converting a single byte
To compare, you can do:
thatbyte == b'\xd3'
Thus compare a byte object with another byte object.
The shift << operator works on int only
To convert an int back to bytes (assuming it is [0..255]) you can use:
bytes([byteint]) # note the extra brackets!
And as for improvements, I would suggest to read the whole binary file at once:
with open("COM6_200417.ubx", "rb") as f:
allbytes = f.read() # read all
for val in allbytes:
# Do stuff with val, val is int !!!
print(bytes([val]))

Encoding a file with ord function

I'm trying to encode a file and output the encode into a new file, but I got this error:
TypeError: ord() expected string of length 1, but int found
My code:
from sys import argv, exit
def encode(data):
encoded = ''
while data:
current = data[0]
count = 1
for i in data[1:]:
if i == current:
count += 1
else:
break
if count == 255:
break
encoded += '{}{}'.format(chr(ord(current) & 255), chr(count & 255)) #error occurs here.
data = data[count:]
return encoded
if __name__ == '__main__':
if len(argv) < 2:
print('Please specify input file!')
exit(0)
with open(argv[1], 'rb') as (f):
data = f.read()
with open(argv[1] + '.out', 'wb') as (f):
f.write(encode(data))
Additional question: How do I decode the encoded file?
You are reading bytes (open(..., 'rb')), so when you take one element of the byte string, you get a byte, ie. a number. This number already is the character code, so just leave out the ord. Alternatively, you could open the file without the b modifier (open(..., 'r')), which will return a string; I would advise to keep it as a byte string though (or you could run into encoding issues if you are parsing something non-ascii).
You will run into a similar problem saving your file: you cannot write a string into a file opened with the b modifier. Since you have characters outside the ascii range (>128), writing as a string is not a good idea, since python will try to encode your characters (eg. in UTF-8), and you will end up with completely different bytes. Therefore, the best solution probably is not to concat your data to a string in your loop (the part where you do '{}{}'.format(...), but to have a list (encoded = [], concat with encoded.append(current)) and convert that to a byte string using bytes(encoded) after your loop. You can then pass that to write without a problem.
As for how to decode your file, you can just open the file like you do for encoding, read two bytes b1 and b2, and append [b1]*b2 to your output (again, as a list), and convert that to a byte string with bytes().

Python Hex values in ascii encoded string

I have a problem in python reading a string from a .txt file
File contains these data : \xce\x97
Encoded in ascii (Similar to "\xce\x97" using a python string)
I want to convert it to UTF-8 encoding
file.open("file.txt", "r")
a = file.read() #a = "\\xce\\x97"
file.close()
The correct value of this string is : "Η" (Its a greek letter, capital "η")
Ι can use
>>>a = b'\xce\x97'
>>>print(a.decode("utf-8"))
>>>Η
How can I do it using the varriable a?
For decoding problems:
a = "\\xce\\x97"
print(a.encode().decode('unicode-escape').encode("latin-1").decode('utf-8'))
'Η'

(Python) Formatting strings for struct.unpack?

I have a .csv file of values 0-255 that are simulating the data coming off an attitude sensor I'm looking at buying. I'm trying to write a data parser for it using this simulated data. The data will be transmitted over a serial port and packed into a byte array. I've looked into using struct.unpack to decode the data, and it makes sense to me, but I'm having trouble getting the data formatted correctly to run through struct.unpack. Here's an example:
The year is packed as an unsigned short (2 hex values). "2014" would then be ('0xde', '0x07'). Converting to dec, this gives me ('222', '7'), which is what's in my csv file. The problem is, doing
hex(222)+hex(7)
gives me
'0xde0x7'
But I can't put that in here
unpack('H', '0xde0x7')
because it's formatted incorrectly. It needs to be
unpack('H', '\xde\x07')
Here's my current approach that's not working:
dataList = reader(open('testdata.csv', "rb"), delimiter = ",", skipinitialspace=True)
dataList = [[eval(row[0])] for row in dataList]
dataList = [hex(row[0]) for row in dataList]
year = unpack('H', dataList[1]+dataList[2]))
Somehow I need to replace all the leading '0' with '\', then pad any number less than 15 (hex F) with a leading 0. Is there an elegant way to do this, or am I over-thinking it?
How about using bytearray:
bytearray([222,7])
Out[15]: bytearray(b'\xde\x07')
struct.unpack('H', bytearray([222,7]))
Out[16]: (2014,)
In your case, coming from a csv reader, something like
row = ['222','7']
struct.unpack('H', bytearray(map(int,row)))
Out[24]: (2014,)

Categories

Resources