Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text file having hex numbers which is meant for my matrix data.
The first line denotes the rows and cols of my matrices.
This is my input data
00080008 #Rows=0x0008 n cols=0x0008
0024FFF2 #LSB1
004AFFB6 #MSB1
00200030 #LSB2
FFA7FFF0 #MSB2
00480051
002FFF9C
FFF0004B
003BFFD0 
FFA90016
FFD00034
FFA7FFB8
0017FFEE
FFE0FFB2
00050050
0046FFBD
0057FFBF
FFEFFFF8
0044FFFE
0007FFB9
0015FFCD
FFCCFFCE
FFA80028
0057FFB5
002CFFEB
FFAAFFA6
FFFC0010
FFF2FFBC
0051005A
FFB5FFC3
00630035
FFF70035
FFDB002E
FFD2005D
00310059
FFA20003
FFF8FFD7
FFD0FFDA
FFEB0059
FFFEFFE7
FFDA0013
FFD6FFFB
0004FFD4
004AFFA6
0020000E
FFF1005B
FFF4FFFB
00420050
FF9DFFD8
FFF5002E
FFD80034
FFD70023
0034FFC5
003F0028
FFABFFBC
FFD0001A
0061FFA4
0027004F
005D002B
FFEB0027
FFD5FFFC
FFFA002B
FFEFFFD8
FFC4FFBF
004D005D
FFA7005E
FFA60046
0042FFEB
FFB80002
FFE8FFFE
FFE40039
002DFFAB
003CFFAD
3658F5F7 #skip
2B5A10B7 #skip
FD23941D #skip
3A1A23CF #skip
9F161FC6 #skip
BE5E2C70 #skip
4DD10283 #skip
378D4F41 #skip
00080008 #Next iteration
FFA8002D
001B004F
FFABFFEA
004DFFAF
001CFFF8
0008FFF5
0046002A
0016FFAA
FFBCFFA2
0006FFA4
FFAA005B
0049FFF7
FFF8004C
FFBC004C
FFB30024
FFFA0028
FF9C002E
FFE2FFB5
001FFFAF
002F0046
0062FFE4
0022FFF9
FFBC0044
0058003F
0063002A
FFDD000E
00400006
005A001A
0007FFF7
003BFFEC
0018002D
0052FFD8
FFBE0034
FFB7FFA3
FFBA004B
0003002F
0036FFD0
00470015
FFA2002D
0018FF9C
003AFFA0
004A0017
FFB1005E
0007FFF6
00510012
FFC80053
FFCE001D
00180029
FFC0FFD1
FFA0FFD6
FFD2005C
00120050
FFEF003B
FFDA0014
FFDBFFD4
FFA10058
FFBF0000
FFD0005E
00080009
00550036
FFCE0041
001D0034
00110062
FFF8001A
0051FFF7
FFAD0029
001E0034
FFA6FFCC
FFE9FFE8
0051FFAE
00080047
FF9F0057
7102EC67 #skip
4AE2D357 #skip
D765CA63 #skip
16EB9F62 #skip
278A2670 #skip
6929F93E #skip
195D0CE8 #skip
A67D29E0 #skip

My code needs the following:
the rows and coloumn line needs to be filled with "0"s to make it 128 bits
the lines following this line (cols + (rows * cols)+ 1) = 8 + 64 + 1 = 72 lines needs to be merged 4 lines at a time.

I need to combine 4 lines from here and make a hex value MSB2+LSB2+MSB1+LSB1

I have written a code for a pattern which repeats twice but due to some changes in the Master code i get the pattern repeating more than 2 times..How can i modify my code to take care of the repeating pattern?

What I have tried:

with open (filename, 'r') as f1:
    line_values=[line.strip() for line in f1]
f1.close()
width = line_values[0]
width = int(width,16)
rows = (width & 0xffff0000)>>16
cols = (width & 0x0000ffff)
print rows
print cols
counter = cols + (rows * cols) + 1
print counter
data_1 = line_values[:counter]
data_2 = line_values[counter + cols:- cols]

mem_file.write(line_values[0].zfill(32)+"\n")
index = 1

for i in range(int(counter/4)):
    mem_file.write("".join(data_1[(index + 3):(index-1):-1]))
    mem_file.write("\n")
    index += 4
mem_file.write(line_values[counter + rows].zfill(32)+"\n")
index = 1
for i in range(int(counter/4)):
    mem_file.write("".join(data_2[(index + 3):(index-1):-1]))
    mem_file.write("\n")
    index += 4


my data_1 and data_2
['00080008', '0024FFF2', '004AFFB6', '00200030', 'FFA7FFF0', '00480051', '002FFF9C', 'FFF0004B', '003BFFD0', 'FFA90016', 'FFD00034', 'FFA7FFB8', '0017FFEE', 'FFE0FFB2', '00050050', '0046FFBD', '0057FFBF', 'FFEFFFF8', '0044FFFE', '0007FFB9', '0015FFCD', 'FFCCFFCE', 'FFA80028', '0057FFB5', '002CFFEB', 'FFAAFFA6', 'FFFC0010', 'FFF2FFBC', '0051005A', 'FFB5FFC3', '00630035', 'FFF70035', 'FFDB002E', 'FFD2005D', '00310059', 'FFA20003', 'FFF8FFD7', 'FFD0FFDA', 'FFEB0059', 'FFFEFFE7', 'FFDA0013', 'FFD6FFFB', '0004FFD4', '004AFFA6', '0020000E', 'FFF1005B', 'FFF4FFFB', '00420050', 'FF9DFFD8', 'FFF5002E', 'FFD80034', 'FFD70023', '0034FFC5', '003F0028', 'FFABFFBC', 'FFD0001A', '0061FFA4', '0027004F', '005D002B', 'FFEB0027', 'FFD5FFFC', 'FFFA002B', 'FFEFFFD8', 'FFC4FFBF', '004D005D', 'FFA7005E', 'FFA60046', '0042FFEB', 'FFB80002', 'FFE8FFFE', 'FFE40039', '002DFFAB', '003CFFAD']
['00080008', 'FFA8002D', '001B004F', 'FFABFFEA', '004DFFAF', '001CFFF8', '0008FFF5', '0046002A', '0016FFAA', 'FFBCFFA2', '0006FFA4', 'FFAA005B', '0049FFF7', 'FFF8004C', 'FFBC004C', 'FFB30024', 'FFFA0028', 'FF9C002E', 'FFE2FFB5', '001FFFAF', '002F0046', '0062FFE4', '0022FFF9', 'FFBC0044', '0058003F', '0063002A', 'FFDD000E', '00400006', '005A001A', '0007FFF7', '003BFFEC', '0018002D', '0052FFD8', 'FFBE0034', 'FFB7FFA3', 'FFBA004B', '0003002F', '0036FFD0', '00470015', 'FFA2002D', '0018FF9C', '003AFFA0', '004A0017', 'FFB1005E', '0007FFF6', '00510012', 'FFC80053', 'FFCE001D', '00180029', 'FFC0FFD1', 'FFA0FFD6', 'FFD2005C', '00120050', 'FFEF003B', 'FFDA0014', 'FFDBFFD4', 'FFA10058', 'FFBF0000', 'FFD0005E', '00080009', '00550036', 'FFCE0041', '001D0034', '00110062', 'FFF8001A', '0051FFF7', 'FFAD0029', '001E0034', 'FFA6FFCC', 'FFE9FFE8', '0051FFAE', '00080047', 'FF9F0057']'


My txt file data:
00000000000000000000000000080008
FFA7FFF000200030004AFFB60024FFF2
003BFFD0FFF0004B002FFF9C00480051
0017FFEEFFA7FFB8FFD00034FFA90016
0057FFBF0046FFBD00050050FFE0FFB2
0015FFCD0007FFB90044FFFEFFEFFFF8
002CFFEB0057FFB5FFA80028FFCCFFCE
0051005AFFF2FFBCFFFC0010FFAAFFA6
FFDB002EFFF7003500630035FFB5FFC3
FFF8FFD7FFA2000300310059FFD2005D
FFDA0013FFFEFFE7FFEB0059FFD0FFDA
0020000E004AFFA60004FFD4FFD6FFFB
FF9DFFD800420050FFF4FFFBFFF1005B
0034FFC5FFD70023FFD80034FFF5002E
0061FFA4FFD0001AFFABFFBC003F0028
FFD5FFFCFFEB0027005D002B0027004F
004D005DFFC4FFBFFFEFFFD8FFFA002B
FFB800020042FFEBFFA60046FFA7005E
003CFFAD002DFFABFFE40039FFE8FFFE
00000000000000000000000000080008
004DFFAFFFABFFEA001B004FFFA8002D
0016FFAA0046002A0008FFF5001CFFF8
0049FFF7FFAA005B0006FFA4FFBCFFA2
FFFA0028FFB30024FFBC004CFFF8004C
002F0046001FFFAFFFE2FFB5FF9C002E
0058003FFFBC00440022FFF90062FFE4
005A001A00400006FFDD000E0063002A
0052FFD80018002D003BFFEC0007FFF7
0003002FFFBA004BFFB7FFA3FFBE0034
0018FF9CFFA2002D004700150036FFD0
0007FFF6FFB1005E004A0017003AFFA0
00180029FFCE001DFFC8005300510012
00120050FFD2005CFFA0FFD6FFC0FFD1
FFA10058FFDBFFD4FFDA0014FFEF003B
0055003600080009FFD0005EFFBF0000
FFF8001A00110062001D0034FFCE0041
FFA6FFCC001E0034FFAD00290051FFF7
FF9F0057000800470051FFAEFFE9FFE8



This code perfectly works for any pattern repeating twice. As i split the data into 2 lists and then use these lists items to combine and write in my txt file. How do I modify the code if the pattern repeats n number of times?
Posted
Updated 13-Oct-20 16:39pm
v4
Comments
[no name] 13-Oct-20 14:06pm    
It "sounds" like it should be easy, but I don't think anybody understands what you want or what you're doing or what the point is.
Member 11362771 13-Oct-20 22:40pm    
Yeah...found a way out

1 solution

try:
		with open (filename, 'r') as f1:
			line_values=[line.strip() for line in f1]
		f1.close()	
		# print len(line_values)
		index = 0
		while index < len(line_values):
			
			count = line_values[index]
			width = line_values[index]
			width = int(width,16)
			rows = (width & 0xffff0000)>>16
			cols = (width & 0x0000ffff)
			counter = cols + rows * cols + rows
			mem_file.write(count.zfill(32)+"\n")
			
			index += 1
			data_count = counter - rows
			# print data_count
			for i in range(int(data_count / 4)):
				mem_file.write("".join(line_values[(index + 3):(index-1):-1]))
				mem_file.write("\n")
				index += 4
			index += rows
				
		print "Done"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900