无符号字符数组问题

时间:2019-01-21 17:56:34

标签: python c++

我有工作正常的C ++代码,将一位字体位移了一点。我试图让Python代码执行相同的操作。感谢您的任何帮助。 我正在重新编写我的问题,以将其简化为我认为确实需要的内容。 非常感谢您的时间和指示。

在C ++中,我可以创建:

unsigned char   vals[8][8] = {0};

哪个给了我一个8 X 8的数组,其中填充了0'...表示vals [0] [0]的{0}

,数组将转到vals [7] [7]

在我的C ++代码中,我用位字体值填充vals数组,然后将值<< = 1移位,例如:

vals[5][3] <<= 1 

我需要在Python中使用相同的无符号char 2D数组,并且能够移动Python数组中的值(加载了与C ++数组中相同的值),并从Python数组中获得与我相同的移位值从C ++数组中获取。

我对任何提供与C ++相同值的Python数组都没有成功。我需要获得帮助

unsigned char   vals[8][8] = {0};

进入Python。 我曾尝试创建和填充不同的数组,字典,列表,但没有成功。

我想补充一点,这是我试图在Python中实现的C ++的工作副本。可以编译。

#include <stdio.h>
#define STRING               "12345678"
unsigned char *text = STRING;

const unsigned char TEST_FONT[128][8] = {
        { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
        { 0x00, 0x3E, 0x41, 0x55, 0x41, 0x55, 0x49, 0x3E },
        { 0x00, 0x3E, 0x7F, 0x6B, 0x7F, 0x6B, 0x77, 0x3E },
        { 0x00, 0x22, 0x77, 0x7F, 0x7F, 0x3E, 0x1C, 0x08 },
        { 0x00, 0x08, 0x1C, 0x3E, 0x7F, 0x3E, 0x1C, 0x08 },
        { 0x00, 0x08, 0x1C, 0x2A, 0x7F, 0x2A, 0x08, 0x1C },
        { 0x00, 0x08, 0x1C, 0x3E, 0x7F, 0x3E, 0x08, 0x1C },
        { 0x00, 0x00, 0x1C, 0x3E, 0x3E, 0x3E, 0x1C, 0x00 },
        { 0xFF, 0xFF, 0xE3, 0xC1, 0xC1, 0xC1, 0xE3, 0xFF },
        { 0x00, 0x00, 0x1C, 0x22, 0x22, 0x22, 0x1C, 0x00 },
        { 0xFF, 0xFF, 0xE3, 0xDD, 0xDD, 0xDD, 0xE3, 0xFF },
        { 0x00, 0x0F, 0x03, 0x05, 0x39, 0x48, 0x48, 0x30 },
        { 0x00, 0x08, 0x3E, 0x08, 0x1C, 0x22, 0x22, 0x1C },
        { 0x00, 0x18, 0x14, 0x10, 0x10, 0x30, 0x70, 0x60 },
        { 0x00, 0x0F, 0x19, 0x11, 0x13, 0x37, 0x76, 0x60 },
        { 0x00, 0x08, 0x2A, 0x1C, 0x77, 0x1C, 0x2A, 0x08 },
        { 0x00, 0x60, 0x78, 0x7E, 0x7F, 0x7E, 0x78, 0x60 },
        { 0x00, 0x03, 0x0F, 0x3F, 0x7F, 0x3F, 0x0F, 0x03 },
        { 0x00, 0x08, 0x1C, 0x2A, 0x08, 0x2A, 0x1C, 0x08 },
        { 0x00, 0x66, 0x66, 0x66, 0x66, 0x00, 0x66, 0x66 },
        { 0x00, 0x3F, 0x65, 0x65, 0x3D, 0x05, 0x05, 0x05 },
        { 0x00, 0x0C, 0x32, 0x48, 0x24, 0x12, 0x4C, 0x30 },
        { 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x7F, 0x7F },
        { 0x00, 0x08, 0x1C, 0x2A, 0x08, 0x2A, 0x1C, 0x3E },
        { 0x00, 0x08, 0x1C, 0x3E, 0x7F, 0x1C, 0x1C, 0x1C },
        { 0x00, 0x1C, 0x1C, 0x1C, 0x7F, 0x3E, 0x1C, 0x08 },
        { 0x00, 0x08, 0x0C, 0x7E, 0x7F, 0x7E, 0x0C, 0x08 },
        { 0x00, 0x08, 0x18, 0x3F, 0x7F, 0x3F, 0x18, 0x08 },
        { 0x00, 0x00, 0x00, 0x70, 0x70, 0x70, 0x7F, 0x7F },
        { 0x00, 0x00, 0x14, 0x22, 0x7F, 0x22, 0x14, 0x00 },
        { 0x00, 0x08, 0x1C, 0x1C, 0x3E, 0x3E, 0x7F, 0x7F },
        { 0x00, 0x7F, 0x7F, 0x3E, 0x3E, 0x1C, 0x1C, 0x08 },
        { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
        { 0x00, 0x18, 0x3C, 0x3C, 0x18, 0x18, 0x00, 0x18 },
        { 0x00, 0x36, 0x36, 0x14, 0x00, 0x00, 0x00, 0x00 },
        { 0x00, 0x36, 0x36, 0x7F, 0x36, 0x7F, 0x36, 0x36 },
        { 0x00, 0x08, 0x1E, 0x20, 0x1C, 0x02, 0x3C, 0x08 },
        { 0x00, 0x60, 0x66, 0x0C, 0x18, 0x30, 0x66, 0x06 },
        { 0x00, 0x3C, 0x66, 0x3C, 0x28, 0x65, 0x66, 0x3F },
        { 0x00, 0x18, 0x18, 0x18, 0x30, 0x00, 0x00, 0x00 },
        { 0x00, 0x06, 0x0C, 0x18, 0x18, 0x18, 0x0C, 0x06 },
        { 0x00, 0x60, 0x30, 0x18, 0x18, 0x18, 0x30, 0x60 },
        { 0x00, 0x00, 0x36, 0x1C, 0x7F, 0x1C, 0x36, 0x00 },
        { 0x00, 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08, 0x00 },
        { 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x30, 0x60 },
        { 0x00, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00 },
        { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x60 },
        { 0x00, 0x00, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x00 },
        { 0x00, 0x3C, 0x66, 0x6E, 0x76, 0x66, 0x66, 0x3C },
        { 0x00, 0x18, 0x18, 0x38, 0x18, 0x18, 0x18, 0x7E },
        { 0x00, 0x3C, 0x66, 0x06, 0x0C, 0x30, 0x60, 0x7E },
        { 0x00, 0x3C, 0x66, 0x06, 0x1C, 0x06, 0x66, 0x3C },
        { 0x00, 0x0C, 0x1C, 0x2C, 0x4C, 0x7E, 0x0C, 0x0C },
        { 0x00, 0x7E, 0x60, 0x7C, 0x06, 0x06, 0x66, 0x3C },
        { 0x00, 0x3C, 0x66, 0x60, 0x7C, 0x66, 0x66, 0x3C },
        { 0x00, 0x7E, 0x66, 0x0C, 0x0C, 0x18, 0x18, 0x18 },
        { 0x00, 0x3C, 0x66, 0x66, 0x3C, 0x66, 0x66, 0x3C },
        { 0x00, 0x3C, 0x66, 0x66, 0x3E, 0x06, 0x66, 0x3C },
        { 0x00, 0x00, 0x18, 0x18, 0x00, 0x18, 0x18, 0x00 },
        { 0x00, 0x00, 0x18, 0x18, 0x00, 0x18, 0x18, 0x30 },
        { 0x00, 0x06, 0x0C, 0x18, 0x30, 0x18, 0x0C, 0x06 },
        { 0x00, 0x00, 0x00, 0x3C, 0x00, 0x3C, 0x00, 0x00 },
        { 0x00, 0x60, 0x30, 0x18, 0x0C, 0x18, 0x30, 0x60 },
        { 0x00, 0x3C, 0x66, 0x06, 0x1C, 0x18, 0x00, 0x18 },
        { 0x00, 0x38, 0x44, 0x5C, 0x58, 0x42, 0x3C, 0x00 },
        { 0x00, 0x3C, 0x66, 0x66, 0x7E, 0x66, 0x66, 0x66 },
        { 0x00, 0x7C, 0x66, 0x66, 0x7C, 0x66, 0x66, 0x7C },
        { 0x00, 0x3C, 0x66, 0x60, 0x60, 0x60, 0x66, 0x3C },
        { 0x00, 0x7C, 0x66, 0x66, 0x66, 0x66, 0x66, 0x7C },
        { 0x00, 0x7E, 0x60, 0x60, 0x7C, 0x60, 0x60, 0x7E },
        { 0x00, 0x7E, 0x60, 0x60, 0x7C, 0x60, 0x60, 0x60 },
        { 0x00, 0x3C, 0x66, 0x60, 0x60, 0x6E, 0x66, 0x3C },
        { 0x00, 0x66, 0x66, 0x66, 0x7E, 0x66, 0x66, 0x66 },
        { 0x00, 0x3C, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C },
        { 0x00, 0x1E, 0x0C, 0x0C, 0x0C, 0x6C, 0x6C, 0x38 },
        { 0x00, 0x66, 0x6C, 0x78, 0x70, 0x78, 0x6C, 0x66 },
        { 0x00, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7E },
        { 0x00, 0x63, 0x77, 0x7F, 0x6B, 0x63, 0x63, 0x63 },
        { 0x00, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x63, 0x63 },
        { 0x00, 0x3C, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3C },
        { 0x00, 0x7C, 0x66, 0x66, 0x66, 0x7C, 0x60, 0x60 },
        { 0x00, 0x3C, 0x66, 0x66, 0x66, 0x6E, 0x3C, 0x06 },
        { 0x00, 0x7C, 0x66, 0x66, 0x7C, 0x78, 0x6C, 0x66 },
        { 0x00, 0x3C, 0x66, 0x60, 0x3C, 0x06, 0x66, 0x3C },
        { 0x00, 0x7E, 0x5A, 0x18, 0x18, 0x18, 0x18, 0x18 },
        { 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3E },
        { 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3C, 0x18 },
        { 0x00, 0x63, 0x63, 0x63, 0x6B, 0x7F, 0x77, 0x63 },
        { 0x00, 0x63, 0x63, 0x36, 0x1C, 0x36, 0x63, 0x63 },
        { 0x00, 0x66, 0x66, 0x66, 0x3C, 0x18, 0x18, 0x18 },
        { 0x00, 0x7E, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x7E },
        { 0x00, 0x1E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1E },
        { 0x00, 0x00, 0x60, 0x30, 0x18, 0x0C, 0x06, 0x00 },
        { 0x00, 0x78, 0x18, 0x18, 0x18, 0x18, 0x18, 0x78 },
        { 0x00, 0x08, 0x14, 0x22, 0x41, 0x00, 0x00, 0x00 },
        { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F },
        { 0x00, 0x0C, 0x0C, 0x06, 0x00, 0x00, 0x00, 0x00 },
        { 0x00, 0x00, 0x00, 0x3C, 0x06, 0x3E, 0x66, 0x3E },
        { 0x00, 0x60, 0x60, 0x60, 0x7C, 0x66, 0x66, 0x7C },
        { 0x00, 0x00, 0x00, 0x3C, 0x66, 0x60, 0x66, 0x3C },
        { 0x00, 0x06, 0x06, 0x06, 0x3E, 0x66, 0x66, 0x3E },
        { 0x00, 0x00, 0x00, 0x3C, 0x66, 0x7E, 0x60, 0x3C },
        { 0x00, 0x1C, 0x36, 0x30, 0x30, 0x7C, 0x30, 0x30 },
        { 0x00, 0x00, 0x3E, 0x66, 0x66, 0x3E, 0x06, 0x3C },
        { 0x00, 0x60, 0x60, 0x60, 0x7C, 0x66, 0x66, 0x66 },
        { 0x00, 0x00, 0x18, 0x00, 0x18, 0x18, 0x18, 0x3C },
        { 0x00, 0x0C, 0x00, 0x0C, 0x0C, 0x6C, 0x6C, 0x38 },
        { 0x00, 0x60, 0x60, 0x66, 0x6C, 0x78, 0x6C, 0x66 },
        { 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18 },
        { 0x00, 0x00, 0x00, 0x63, 0x77, 0x7F, 0x6B, 0x6B },
        { 0x00, 0x00, 0x00, 0x7C, 0x7E, 0x66, 0x66, 0x66 },
        { 0x00, 0x00, 0x00, 0x3C, 0x66, 0x66, 0x66, 0x3C },
        { 0x00, 0x00, 0x7C, 0x66, 0x66, 0x7C, 0x60, 0x60 },
        { 0x00, 0x00, 0x3C, 0x6C, 0x6C, 0x3C, 0x0D, 0x0F },
        { 0x00, 0x00, 0x00, 0x7C, 0x66, 0x66, 0x60, 0x60 },
        { 0x00, 0x00, 0x00, 0x3E, 0x40, 0x3C, 0x02, 0x7C },
        { 0x00, 0x00, 0x18, 0x18, 0x7E, 0x18, 0x18, 0x18 },
        { 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x3E },
        { 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x3C, 0x18 },
        { 0x00, 0x00, 0x00, 0x63, 0x6B, 0x6B, 0x6B, 0x3E },
        { 0x00, 0x00, 0x00, 0x66, 0x3C, 0x18, 0x3C, 0x66 },
        { 0x00, 0x00, 0x00, 0x66, 0x66, 0x3E, 0x06, 0x3C },
        { 0x00, 0x00, 0x00, 0x3C, 0x0C, 0x18, 0x30, 0x3C },
        { 0x00, 0x0E, 0x18, 0x18, 0x30, 0x18, 0x18, 0x0E },
        { 0x00, 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18 },
        { 0x00, 0x70, 0x18, 0x18, 0x0C, 0x18, 0x18, 0x70 },
        { 0x00, 0x00, 0x00, 0x3A, 0x6C, 0x00, 0x00, 0x00 },
        { 0x00, 0x08, 0x1C, 0x36, 0x63, 0x41, 0x41, 0x7F }
};

#define FONT    TEST_FONT
int length = 8;
unsigned char   vals[8][8] = { 0 };

void font_update(unsigned char *loc)
{
    int     i, j, x = 0, pos = 0;
    while(*loc != 0x00) {
        for(i = 0; i < 8; i++)  {
            vals[pos][i] = FONT[*loc][i];
        }
        pos++; loc++;
    }
    while(x < 4) {
        for(j = 0; j < length; j++)   {
            for(i = 0; i < 8; i++)  {
                printf (" Shift Before Vals[%d][%d] = %d \n",j,i,vals[j][i]);
                vals[j][i] <<= 1;
                printf (" Shift After Vals[%d][%d] = %d \n",j,i,vals[j][i]);
                //an "if" block is not provided the curly braces then only 1 statement is considered inside it.
                if((j != length -1) && (vals[j + 1][i] & 0x80)) {
                    vals[j][i] |= 0x01;
                    printf (" Length Update Vals[%d][%d] = %d \n",j,i,vals[j][i]);
                }
            printf ("\n");
            }
        }
        x++;
    }
};

int main ()
{

int x;
    for(x=0; x < 5; x++) {
      font_update(text);
    }
}

1 个答案:

答案 0 :(得分:0)

您可能要使用Select [letter] From ( Values ('A') ,('B') ,('C') ) As [Letters]([letter]) 和指定类型的ndarray-例如

NumPy

与默认的Python import numpy as np np.full(shape=8, fill_value=0, dtype=np.int8) # or np.zeros() 处理相反(iBug在注释中指出-无限长),您将得到非常特定的位长的整数值。转移这些将使您的行为类似于C ++。