python 格式化文本输出十六进制数据
作者:互联网
f = open("D:\\hex_out.mot", 'r') #str = f.write("3434") out_str1 = '0x' Input_str = f.readline() Str_num = len(Input_str); Str_num = Str_num -1; Arry_size = Str_num//2 out_str1 = out_str1 + Input_str[0] + Input_str[1] list_loop = list(range(Arry_size -1)) index = 0; for x in range(Arry_size -1): out_str1 = out_str1 + ','+ '0x' + Input_str[index+2] + Input_str[index+3] index = index +2 #print(out_str1) print(out_str1) f.close()
举例
输入文件为:
6756B98C6EE4E7C61B45FA48CB13E0B5357B48CFDFF65E3CC39D1423FF0BD69A4545B3C14EF45CB130864CCC5470366CA743D7FD9B4875659219A7D53541E98E769BAEF6C41D116522D06C9F2E58F191DA91A1B74625A73FDCEFA5644B24BA3318FACA2736B83C39E415AF9DB7A69FF8E93D7EE18D0FEA228F2367B6780BCE3C80E94C47BA3F0E7259C48A1D3EE81E15E63B676302281008380018187746C570B7C7474E02FA3DCADF134244B519464D4B3958D48BA1C928162AF4FF7DC2BAE999EC8D947BDC9C7B4E9476150338D864C446124AE1934CF8DA79B7F6276DCE312F7472F06A5B7E0ACC459B986540CE9157B1035B13743D0D15E7F8AF8D6698F9
输出为:
0x67,0x56,0xB9,0x8C,0x6E,0xE4,0xE7,0xC6,0x1B,0x45,0xFA,0x48,0xCB,0x13,0xE0,0xB5,0x35,0x7B,0x48,0xCF,0xDF,0xF6,0x5E,0x3C,0xC3,0x9D,0x14,0x23,0xFF,0x0B,0xD6,0x9A,0x45,0x45,0xB3,0xC1,0x4E,0xF4,0x5C,0xB1,0x30,0x86,0x4C,0xCC,0x54,0x70,0x36,0x6C,0xA7,0x43,0xD7,0xFD,0x9B,0x48,0x75,0x65,0x92,0x19,0xA7,0xD5,0x35,0x41,0xE9,0x8E,0x76,0x9B,0xAE,0xF6,0xC4,0x1D,0x11,0x65,0x22,0xD0,0x6C,0x9F,0x2E,0x58,0xF1,0x91,0xDA,0x91,0xA1,0xB7,0x46,0x25,0xA7,0x3F,0xDC,0xEF,0xA5,0x64,0x4B,0x24,0xBA,0x33,0x18,0xFA,0xCA,0x27,0x36,0xB8,0x3C,0x39,0xE4,0x15,0xAF,0x9D,0xB7,0xA6,0x9F,0xF8,0xE9,0x3D,0x7E,0xE1,0x8D,0x0F,0xEA,0x22,0x8F,0x23,0x67,0xB6,0x78,0x0B,0xCE,0x3C,0x80,0xE9,0x4C,0x47,0xBA,0x3F,0x0E,0x72,0x59,0xC4,0x8A,0x1D,0x3E,0xE8,0x1E,0x15,0xE6,0x3B,0x67,0x63,0x02,0x28,0x10,0x08,0x38,0x00,0x18,0x18,0x77,0x46,0xC5,0x70,0xB7,0xC7,0x47,0x4E,0x02,0xFA,0x3D,0xCA,0xDF,0x13,0x42,0x44,0xB5,0x19,0x46,0x4D,0x4B,0x39,0x58,0xD4,0x8B,0xA1,0xC9,0x28,0x16,0x2A,0xF4,0xFF,0x7D,0xC2,0xBA,0xE9,0x99,0xEC,0x8D,0x94,0x7B,0xDC,0x9C,0x7B,0x4E,0x94,0x76,0x15,0x03,0x38,0xD8,0x64,0xC4,0x46,0x12,0x4A,0xE1,0x93,0x4C,0xF8,0xDA,0x79,0xB7,0xF6,0x27,0x6D,0xCE,0x31,0x2F,0x74,0x72,0xF0,0x6A,0x5B,0x7E,0x0A,0xCC,0x45,0x9B,0x98,0x65,0x40,0xCE,0x91,0x57,0xB1,0x03,0x5B,0x13,0x74,0x3D,0x0D,0x15,0xE7,0xF8,0xAF,0x8D,0x66,0x98,0xF9
标签:十六进制,格式化,index,python,str1,str,Input,0xE9,out 来源: https://www.cnblogs.com/hkj8808/p/16401087.html