tags:

views:

382

answers:

2

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
+1  A: 

Are you looking for divmod function?

[Edit: using python operators instead of pseudo language.]

char is between 0 and 319

character = (char % 40)
column    = (char / 40) % 5
row       = (char / 40) / 5
Ryan Oberoi
It didn't work for me. I imagine mod's in there somewhere, but so far I'm stumped.
Scott
div is / and mod is % in python. I will edit my response to reflect that.
Ryan Oberoi
+2  A: 

Okay, this looks as if you're working with a 1x8 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:

 0  1  2  3  4
 5  6  7  8  9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
25 26 27 28 29
30 31 32 33 34
35 36 37 38 39

We then have:

 # Compute which of the 8 characters the pixel falls in, 0..7:
 char = int(number / 40)

 # Compute which pixel column the pixel is in, 0..4:
 col = number % 5

 # Compute which pixel row the pixel is in, 0..7:
 row = int((number - char * 40) / 5)

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.

unwind
You described my situation perfectly, and you answered the question. Thanks.
Scott