The C language implements the bit array in the target space around the center alignment three kinds of ways

Source: Internet
Author: User

In the LED industry, generally a light or not light with a bit to indicate (here is not talking about the colorful or grayscale control card), if our screen size is 128 points, equivalent to the width of 16 bytes, if we let two Chinese characters Center display (two Chinese characters in the width of 4 bytes), it is easy to calculate, as long as the offset (16 -4)/2 = 6 byte width, of course this is a condition of the illusion, if the character length and width of the display changes arbitrarily, manual calculation seems a bit weak. This article is mainly to solve this problem, the implementation of the effect has, character width arbitrarily set, the number of characters is less than 256, but also can be set on the left, right and center display, code reading may not be so smooth, write it is not smooth, after repeated testing did not have any problems.

#include <QCoreApplication> #include <qdebug>/******************************************************* * From a high point in byte 0xff to intercept the length (0~8) in the direction of the low, the resulting content * such as starting from BIT6 to the low length of 2, we can quickly learn that for 0x60 * This is 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, 0  x7e, 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, 0x0 0, 0x00}, {0x00, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0 x00},};/******************************************************************************** * *dest: Target space head pointer * *SRC: The character that needs to be populated with the content header pointer * *width, the character width header pointer that needs to be populated * CharNum: The character length to fill * SCREenwidth: Total length of the target space (calculated as bit bit) * Align: 0 (left-aligned) 1 (centered on it) 2 (right to it) **************************************************** /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)//calculates the length of all padding bits {allcharwidth + = Charwidth[count];    count++;    } if (Allcharwidth > ScreenWidth)//If the total length is greater than the screen length, then pass it over at a speed error.    {return 1;            } switch (align) {case 1://Center Display offset = (screenwidth-allcharwidth) >> 1;        Break            Case 2://Show offset = screenwidth-allcharwidth on the right;    Break    }//Find the current byte and current bit bit quo = (uint8_t) to be populated (offset >> 3);    rem = (uint8_t) (offset% 8);    Count = 0; while (Count < CharNum) {if (REM + CharwidtH[count] < 8)//If the current fill bit bit is less than 8 with the number of characters that need to be filled, then {//the current padding byte can be fully populated without switching to the next byte fill            Dest[quo] &= ~get_middle_byte[rem][charwidth[count]];//The bit length of the fill space charwidth[count] zeroed.  Dest[quo] |= ((Src[count] & Get_middle_byte[0][charwidth[count]) >> REM);    Padded rem + = Charwidth[count];                              Bit bit fill space move forward the width of the current padding character} else {tmp = 8-rem;    A bit length that is not populated by the current byte, used as a temporary variable dest[quo] &= ~get_middle_byte[rem][tmp];   Rem~bit7 these 0 dest[quo] |= ((Src[count] & get_middle_byte[0][tmp]) >> REM);                                      Filling quo++; Switch to next byte dest[quo] &= ~get_middle_byte[0][charwidth[count]-tmp];//The current padding byte (the remaining unfilled character length) length clear 0 de    St[quo] |= ((Src[count] & Get_middle_byte[tmp][charwidth[count]-tmp]) << (TMP));               Fill rem = Charwidth[count]-tmp; Bit bit to switch back to the newLocation} 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 ();}


The C language implements the bit array in the target space around the center alignment three kinds of ways

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.