Sep-11-2019, 02:53 AM
I'm reading numbers from a file which I want to write to a separate file as the ascii hex of the numbers. So, e.g., if I read in ff, I want to write out '0xff'. My code (below; just printing to console for now) works fine for some values, but not for others. Specifically, it can read/write ff (as above), but when it reads 80, it writes "0x20ac". This seems to be because it interprets 80 as a unicode character whose hex value is 0x20ac (8364 decimal). What am I doing wrong? Misusing hex() and/or ord()? Thanks for any tips.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Open output file with open ( "E:\\Karl\\Documents\\Sculpture\\Prime Time\\Digits\\1d_inv_conv.c" , 'w' ) as g: # Open input file for reading in text mode with open ( "E:\\Karl\\Documents\\Sculpture\\Prime Time\\Digits\\1d_inv2.pbm" , 'rt' ) as f: f.read( 10 ) # Skip header while True : x = f.read( 1 ) if x = = '': # End of file? sys.exit() print ( hex ( ord (x))) |