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();
}
时间: 2024-08-28 22:48:41

C语言实现比特位数组在目标空间左右居中对齐三种方式的相关文章

清空数组的三种方式

清空数组的三种方式: 1.splice(0,数组的长度): var arr1 = arr.splice(0,arr.length); console.log(arr1); 2.让数组的长度为0: 这种方式很有意思,其它语言如Java,其数组的length是只读的,不能被赋值.如   int[] ary = {1,2,3,4}; ary.length = 0; Java中会报错,编译通不过.而JS中则可以,且将数组清空了; (在JS中length的属性是可读可写的,也就是可以给length属性赋值

数组的三种方式总结、 多维数组的遍历及 Arrays类的常用方法总结

一.数组的三种方式总结 1.创建数组 Java语言使用new操作符来创建数组,语法如下: arrayRefVar = new dataType[arraySize]; 上面的语法语句做了两件事: 一.使用 dataType[arraySize] 创建了一个数组. 二.把新创建的数组的引用赋值给变量 arrayRefVar. 数组变量的声明,和创建数组可以用一条语句完成,如下所示: dataType[] arrayRefVar = new dataType[arraySize]; 另外,你还可以使

在Java中判断数组中包含某个元素的几种方式的比较

闲来无事,将java中判断数组中包含某个元素的几种方式的速度进行对比,直接上代码 talk is cheap, show you the code package test.contain.lishaojie; import java.util.Arrays;import java.util.HashSet;import java.util.Set; public class TestContain { /** * @param args */ public static void main(S

java数组扩增的三种方式

java数组声明的时候必须声明其长度,但当我们想对数组进行扩增的时候该怎么办呢? 下面三种方式都可以进行扩增,最后一种也最为方便. 1 /** 2 * 手动循环扩增 3 */ 4 int[] arr = { 1, 2, 3 }; 5 int[] tempArray = new int[arr.length * 2]; 6 for (int i = 0; i < arr.length; i++) { 7 tempArray[i] = arr[i]; 8 } 9 //tempArray = { 1,

JavaScript定义数组的三种方式(new Array(),new Array(&#39;x&#39;,&#39;y&#39;),[&#39;x&#39;,&#39;y&#39;])

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

PHP数组缓存:三种方式JSON、序列化和var_export的比较

使用PHP的站点系统,在面对大数据量的时候不得不引入缓存机制.有一种简单有效的办法是将PHP的对象缓存到文件里.下面我来对这3种缓存方法进行说明和比较. 第一种方法:JSONJSON缓存变量的方式主要是使用json_encode和json_decode两个php函数.json_encode可以将变量变成文本格式,这样就可以存到文件里.使用样例如下: // Store cache file_put_contents($cachePath, json_encode($myDataArray)); /

Java 数组元素倒序的三种方式

将数组元素反转有多种实现方式,这里介绍常见的三种. 直接数组元素对换 @Test public void testReverseSelf() throws Exception { System.out.println("use ReverseSelf"); String[] strings = { "ramer", "jelly", "bean", "cake" }; System.out.println(

C#初始化数组的三种方式

C#声明数组并初始化,有三种方式. 对于一维数组: using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlContro

JS创建对象,数组,函数的三种方式

害怕自己忘记,简单总结一下 创建对象的3种方法 ①:创建一个空对象   var obj = {}; ②:对象字面量 var obj = { name: "Tom", age: 27 } ③:构造函数 (不推荐) var obj = new Object(); 创建数组的3种方法 ①:创建一个空数组 var arr = []; ②:隐式创建 var arr = [1,2,3]; ③:构造函数(不推荐) var arr = new Array(3);  (固定数组长度) arr[0] =