C語言實現位元位元組在目標空間左右置中對齊三種方式,置中對齊三種方式

來源:互聯網
上載者:User

C語言實現位元位元組在目標空間左右置中對齊三種方式,置中對齊三種方式

        在LED行業中,一般一個燈亮或者不亮用一個bit位來表示(這裡就不談七彩或者灰階控制卡),假如我們螢幕大小是128點,相當於寬度16個位元組,如果我們讓兩個漢字置中顯示(兩個漢字占寬度4個位元組),很容易算出,只要位移(16 - 4) / 2 = 6個位元組寬度,當然這是假象的某種狀況,如果顯示的字元長度、寬度任意變化,手動計算好像有點無力。本文主要是來解決這個問題,實現的效果有,字元寬度隨意設定,字元個數小於256,而且還可以設定靠左、靠右和置中顯示,代碼讀起來可能不是那麼順暢,寫起來也不順暢,經反覆測試沒出任何問題。

#include <QCoreApplication>#include <QDebug>/******************************************************************************** * 從位元組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();}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.