c语言让数组地址对齐,C语言实现比特位数组在目标空间左右居中对齐三种方式...

在LED行业中,一般一个灯亮或者不亮用一个bit位来表示(这里就不谈七彩或者灰度控制卡),假如我们屏幕大小是128点,相当于宽度16个字节,如果我们让两个汉字居中显示(两个汉字占宽度4个字节),很容易算出,只要偏移(16 - 4) / 2 = 6个字节宽度,当然这是假象的某种状况,如果显示的字符长度、宽度任意变化,手动计算好像有点无力。本文主要是来解决这个问题,实现的效果有,字符宽度随意设置,字符个数小于256,而且还可以设置靠左、靠右和居中显示,代码读起来可能不是那么顺畅,写起来也不顺畅,经反复测试没出任何问题。

#include

#include

/********************************************************************************

* 从字节0xff某个高位开始往低位的方向截取长度(0~8),所得的内容

* 比如从bit6开始往低位截图长度为2,我们很快可以得知为0x60

* 这里面就是Get_Middle_Byte[7 - 6][2]

********************************************************************************/

const uint8_t Get_Middle_Byte[8][9] = {

{0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff},

{0x00, 0x40, 0x60, 0x70, 0x78, 0x7c, 0x7e, 0x7f, 0x00},

{0x00, 0x20, 0x30, 0x38, 0x3c, 0x3e, 0x3f, 0x00, 0x00},

{0x00, 0x10, 0x18, 0x1c, 0x1e, 0x1f, 0x00, 0x00, 0x00},

{0x00, 0x08, 0x0c, 0x0e, 0x0f, 0x00, 0x00, 0x00, 0x00},

{0x00, 0x04, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00},

{0x00, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},

{0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},

};

/********************************************************************************

* *dest:目标空间头指针

* *src:需要填充的字符内容头指针

* *width,需要填充的字符宽度头指针

* charNum:需要填充的字符长度

* screenWidth:目标空间的总长度(按照bit位计算)

* align:对其方式 0(左对齐) 1(居中对其) 2(靠右对其)

********************************************************************************/

uint8_t Organization_One_Row_Char(uint8_t *dest, uint8_t *src, uint8_t *charWidth, uint8_t charNum, uint16_t screenWidth, uint8_t align)

{

uint8_t count = 0;

uint16_t allCharWidth = 0, offset = 0;

uint8_t quo, rem, tmp;

while (count < charNum) //计算所有填充位的长度

{

allCharWidth += charWidth[count];

count++;

}

if (allCharWidth > screenWidth) //如果总长度大于屏幕长度,那就传递过来速度有错误。

{

return 1;

}

switch (align)

{

case 1: //居中显示

offset = (screenWidth - allCharWidth) >> 1;

break;

case 2: //靠右显示

offset = screenWidth - allCharWidth;

break;

} //求出需要填充的当前字节和当前bit位

quo = (uint8_t)(offset >> 3);

rem = (uint8_t)(offset % 8);

count = 0;

while (count < charNum)

{

if (rem + charWidth[count] < 8) //如果当前填充bit位与需要填充字符相加的值小于8,那就

{ //当前填充字节可以完全填充完,不需要切换下一个字节填充

dest[quo] &= ~Get_Middle_Byte[rem][charWidth[count]];//填充空间的bit位长度charWidth[count]清零。

dest[quo] |= ((src[count] & Get_Middle_Byte[0][charWidth[count]]) >> rem); //填充

rem += charWidth[count]; //bit位填充空间往前移动当前填充字符的宽度

}

else

{

tmp = 8 - rem; //求当前字节未填充的bit长度,用作临时变量

dest[quo] &= ~Get_Middle_Byte[rem][tmp]; //rem~bit7这几位清零

dest[quo] |= ((src[count] & Get_Middle_Byte[0][tmp]) >> rem); //填充

quo++; //切换到下一个字节

dest[quo] &= ~Get_Middle_Byte[0][charWidth[count] - tmp];//当前填充字节(剩余未填充完的字符长度)长度清零

dest[quo] |= ((src[count] & Get_Middle_Byte[tmp][charWidth[count] - tmp]) << (tmp)); //填充

rem = charWidth[count] - tmp; //bit位重新切换到新位置

}

count++;

}

return 0;

}

int main(int argc, char *argv[])

{

QCoreApplication a(argc, argv);

uint8_t t1[] = {0x00, 0x00, 0x00, 0x00, 0x00};

uint8_t t2[] = {0xff, 0xff, 0xff, 0xff};

uint8_t width[] = {7, 8, 8, 8};

Organization_One_Row_Char(t1, t2, width, 4, 40, 1);

for (int i = 0; i < 5; i++)

{

qDebug() << i << hex << t1[i];

}

return a.exec();

}

原文:http://blog.csdn.net/hwb_1988/article/details/45337331

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值