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
總結
以上是生活随笔為你收集整理的c语言让数组地址对齐,C语言实现比特位数组在目标空间左右居中对齐三种方式...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pic10f220 c语言,PIC10F
- 下一篇: w ndows无法识别usb,电脑无法识