2009-06-12 21 views
1

我的数据结构被初始化为如下:将一个值转换成的行,列和焦炭

[[0,0,0,0,0,0,0,0] for x in range(8)] 

8个字符,8行,每行具有用于列5个比特,所以每个整数可以在之间的范围内0和31(含)。

我必须将数字177(可以在0和319之间)转换为char,row和column。

让我再试一次,这次更好的代码示例。没有位被设置。

好的,我添加了相反的问题。也许这会有所帮助。

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 
+0

你的代码看起来像是4x4,反正不清楚177是如何拆分成char,row,col的。你能描述你想做什么吗? – 2009-06-12 04:01:46

+0

这是一个8x5 :)我会做一个更好的例子。 – Scott 2009-06-12 04:09:25

回答

2

好吧,这看起来好像你用的1x8液晶显示器,其中每个字是8行的5个像素的工作。

因此,总共有8 *(8 * 5)= 320像素,并且您想将像素的索引映射到“帧缓冲区”中描述显示内容的位置。

我假设像素分布是这样的(只对第一个字符显示),你最初的循环表明这是正确的是:

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 

我们则有:

# 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) 

我用明确的int() s表明数字是整数。

请注意,您可能需要翻转该列,因为这是从左侧开始编号的。

1

您是否在寻找divmod函数?

[编辑:使用python运营商,而不是伪语言]

char is between 0 and 319 

character = (char % 40) 
column = (char/40) % 5 
row  = (char/40)/5 
+0

它没有为我工作。我想在那里某处有mod,但到目前为止我很难过。 – Scott 2009-06-12 06:46:41