My data structure is initialized as follows:
[[0,0,0,0,0,0,0,0] for x in range(8)]
8 characters, 8 rows, each row has 5 bits for columns, so each integer can be in the range between 0 and 31 inclusive.
I have to convert the number 177 (can be between 0 and 319) into char, row, and column.
Let me try again, this time with a better code example. No bits are set.
Ok, I added the reverse to the problem. Maybe that’ll help.
chars = [[0,0,0,0,0,0,0,0] for x in range(8)]
# reverse solution
for char in range(8):
for row in range(8):
for col in range(5):
n = char * 40 + (row * 5 + col)
chars[char][row] = chars[char][row] ^ [0, 1<<4-col][row < col]
for data in range(320):
char = data / 40
col = (data - char * 40) % 5
row = ?
print "Char %g, Row %g, Col %g" % (char, row, col), chars[char][row] & 1<<4-col
Okay, this looks as if you’re working with a 1×8 LCD display, where each character is 8 rows of 5 pixels.
So, you have a total of 8 * (8 * 5) = 320 pixels, and you want to map the index of a pixel to a position in the “framebuffer” decribing the display’s contents.
I assume pixels are distributed like this (shown for the first char only), your initial loops indicate this is is correct:
We then have:
I used explicit
int()s to make it clear that the numbers are integers.Note that you might want to flip the column, since this numbers them from the left.