ARTICLE AD BOX
I am working on a program to convert CNG files to JPEG and cannot figure out how to write the binary bytes to a file. My test code is:
import struct import os barray = bytearray() s = 'e0e1e2e3e4e5e6e7e8e9eaebecedeeef' i = 0 cnt = len(s) while i < cnt: j = i + 1 num = s[i] + s[j] num = int(num, 16) num = num ^ 239 #xnum = hex(num)[2:] xnum = hex(num) if num < 16: #if > 16, then add the leading 0 to the hex value to ensure 2-digits xnum = xnum[:2] + '0' + xnum[2:] #xnum = '0' + xnum bnum = bytes(xnum, 'utf-8') barray += bnum i = j + 1 print('barray = \n' + str(barray)) outpath = 'C:\\Users\\jeffe\\Programming\\Python\\' filename = 'hexformattest.jpeg' savefilename = os.path.join(outpath + '\\' + filename) binaryfile = open(savefilename, 'wb') binaryfile.write(bytes(barray)) binaryfile.close()This produces the following output:
barray = bytearray(b'0x0f0x0e0x0d0x0c0x0b0x0a0x090x080x070x060x050x040x030x020x010x00')But the data in the file is:

Swapping the comment sign for lines 13 and 14 and lines 16 and 17 produces the following output:
barray = bytearray(b'0f0e0d0c0b0a09080706050403020100')with the file contents being:

Every resource that I've read implies that what I've tried should write the hex values, opcode, per the "wb" in open(), to the file. However, all I'm doing is writing the ASCII codes for the hex values. docs.python.org mentions struct.pack():
This module converts between Python values and C structs represented as Python bytes objects.
But it seems that Python does provide for saving hex as hex to a file, but I'm not getting that.
