C language: Three Ways of alignment of bit arrays in the left-right corner of the target space, and three ways of alignment in the center

Source: Internet
Author: User

C language: Three Ways of alignment of bit arrays in the left-right corner of the target space, and three ways of alignment in the center

In the LED Industry, a light or no light is usually represented by a bit (here we will not talk about the colorful or grayscale Control Card). If the screen size is 128, it is equivalent to 16 bytes in width, if we display two Chinese characters in the center (two Chinese characters occupy 4 bytes in width), it is easy to calculate, as long as the offset (16-4)/2 = 6 bytes width, of course, this is an hypothetical situation. If the length and width of the displayed characters change at will, manual computation may seem a little powerless. This article mainly aims to solve this problem. The effects are as follows: the character width is set at will, the number of characters is less than 256, and the display can be set to left, right, or center, code reading may not be so smooth, and writing may not be smooth. After repeated tests, there is no problem.

# Include <QCoreApplication> # include <QDebug> /***************************** **************************************** * *********** truncate the length (0 ~ 8), the obtained content * For example, the length from bit6 to low is 2, we will soon learn that the value is 0x60 *, where 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: destination space header pointer ** src: character Content Header pointer ** width, character width header pointer * charNum: word to be filled Character length * screenWidth: the total length of the target space (calculated by bit) * align: 0 (left aligned) 1 (center to it) 2 (right to it) **************************************** **************************************** /uint8_t Organization_One_Row_Char (uint8_t * dest, optional * src, uint8_t * charWidth, uint8_t charNum, uint16_t screenWidth, effecalign) {limit count = 0; uint16_t allCharWidth = 0, offset = 0; uint8_t quo, rem, tmp; while (count <charNum) // count Calculate the length of all fill bits {allCharWidth + = charWidth [count]; count ++;} if (allCharWidth> screenWidth) // if the total length is greater than the screen length, the transfer speed is incorrect. {Return 1 ;}switch (align) {case 1: // center display offset = (screenWidth-allCharWidth)> 1; break; case 2: // display offset = screenWidth-allCharWidth; break on the right; // obtain the Current byte to be filled and the current bit quo = (uint8_t) (offset> 3 ); rem = (uint8_t) (offset % 8); count = 0; while (count <charNum) {if (rem + charWidth [count] <8) // if the value of the currently filled bit and the value of the character to be filled are less than 8, then {// The currently filled byte can be fully filled, you do not need to switch the next byte to fill dest [quo] & = ~ Get_Middle_Byte [rem] [charWidth [count]; // The bit length of the filled space charWidth [count] is cleared. Dest [quo] | = (src [count] & Get_Middle_Byte [0] [charWidth [count])> rem); // fill in rem + = charWidth [count]; // The bit fill space Moves forward the width of the current fill character} else {tmp = 8-rem; // calculate the bit length not filled in the Current byte, used as a temporary variable dest [quo] & = ~ Get_Middle_Byte [rem] [tmp]; // rem ~ Bit7: dest [quo] | = (src [count] & Get_Middle_Byte [0] [tmp])> rem); // fill in quo ++; // switch to the next byte dest [quo] & = ~ Get_Middle_Byte [0] [charWidth [count]-tmp]; // The number of currently filled bytes (remaining unfilled characters) dest [quo] | = (src [count] & Get_Middle_Byte [tmp] [charWidth [count]-tmp]) <(tmp )); // fill in rem = charWidth [count]-tmp; // switch the bit back to the new position} 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}; Organization_One_Row_Char (t1, t2, width, 4, 40, 1); for (int I = 0; I <5; I ++) {qDebug () <I 


 

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.