c语言让数组地址对齐,C语言实现比特位数组在目标空间左右居中对齐三种方式...
在LED行業(yè)中,一般一個(gè)燈亮或者不亮用一個(gè)bit位來(lái)表示(這里就不談七彩或者灰度控制卡),假如我們屏幕大小是128點(diǎn),相當(dāng)于寬度16個(gè)字節(jié),如果我們讓兩個(gè)漢字居中顯示(兩個(gè)漢字占寬度4個(gè)字節(jié)),很容易算出,只要偏移(16 - 4) / 2 = 6個(gè)字節(jié)寬度,當(dāng)然這是假象的某種狀況,如果顯示的字符長(zhǎng)度、寬度任意變化,手動(dòng)計(jì)算好像有點(diǎn)無(wú)力。本文主要是來(lái)解決這個(gè)問(wèn)題,實(shí)現(xiàn)的效果有,字符寬度隨意設(shè)置,字符個(gè)數(shù)小于256,而且還可以設(shè)置靠左、靠右和居中顯示,代碼讀起來(lái)可能不是那么順暢,寫起來(lái)也不順暢,經(jīng)反復(fù)測(cè)試沒(méi)出任何問(wèn)題。
#include
#include
/********************************************************************************
* 從字節(jié)0xff某個(gè)高位開(kāi)始往低位的方向截取長(zhǎng)度(0~8),所得的內(nèi)容
* 比如從bit6開(kāi)始往低位截圖長(zhǎng)度為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:目標(biāo)空間頭指針
* *src:需要填充的字符內(nèi)容頭指針
* *width,需要填充的字符寬度頭指針
* charNum:需要填充的字符長(zhǎng)度
* screenWidth:目標(biāo)空間的總長(zhǎng)度(按照bit位計(jì)算)
* align:對(duì)其方式 0(左對(duì)齊) 1(居中對(duì)其) 2(靠右對(duì)其)
********************************************************************************/
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) //計(jì)算所有填充位的長(zhǎng)度
{
allCharWidth += charWidth[count];
count++;
}
if (allCharWidth > screenWidth) //如果總長(zhǎng)度大于屏幕長(zhǎng)度,那就傳遞過(guò)來(lái)速度有錯(cuò)誤。
{
return 1;
}
switch (align)
{
case 1: //居中顯示
offset = (screenWidth - allCharWidth) >> 1;
break;
case 2: //靠右顯示
offset = screenWidth - allCharWidth;
break;
} //求出需要填充的當(dāng)前字節(jié)和當(dāng)前bit位
quo = (uint8_t)(offset >> 3);
rem = (uint8_t)(offset % 8);
count = 0;
while (count < charNum)
{
if (rem + charWidth[count] < 8) //如果當(dāng)前填充bit位與需要填充字符相加的值小于8,那就
{ //當(dāng)前填充字節(jié)可以完全填充完,不需要切換下一個(gè)字節(jié)填充
dest[quo] &= ~Get_Middle_Byte[rem][charWidth[count]];//填充空間的bit位長(zhǎng)度charWidth[count]清零。
dest[quo] |= ((src[count] & Get_Middle_Byte[0][charWidth[count]]) >> rem); //填充
rem += charWidth[count]; //bit位填充空間往前移動(dòng)當(dāng)前填充字符的寬度
}
else
{
tmp = 8 - rem; //求當(dāng)前字節(jié)未填充的bit長(zhǎng)度,用作臨時(shí)變量
dest[quo] &= ~Get_Middle_Byte[rem][tmp]; //rem~bit7這幾位清零
dest[quo] |= ((src[count] & Get_Middle_Byte[0][tmp]) >> rem); //填充
quo++; //切換到下一個(gè)字節(jié)
dest[quo] &= ~Get_Middle_Byte[0][charWidth[count] - tmp];//當(dāng)前填充字節(jié)(剩余未填充完的字符長(zhǎng)度)長(zhǎng)度清零
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
總結(jié)
以上是生活随笔為你收集整理的c语言让数组地址对齐,C语言实现比特位数组在目标空间左右居中对齐三种方式...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: pic10f220 c语言,PIC10F
- 下一篇: w ndows无法识别usb,电脑无法识