首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获取字节流(1s &0)并将它们存储在uint8_t数组中。

获取字节流(1s &0)并将它们存储在uint8_t数组中。
EN

Stack Overflow用户
提问于 2022-03-13 21:47:54
回答 1查看 277关注 0票数 0

我试图获取这个字节信息流(1和0),我试图将其存储在一个uint8_t数组中(每个索引都是一个字节长,8位)。我很难掌握如何将8位存储到给定bitmapSize的一个索引位置(在本例中是4)。

因此,我们将有一个大小为4的uint8_t数组,其输出如下所示:

数组输出-> 11111100 00011110 00001111 01

任何帮助都将不胜感激!!谢谢!

代码语言:javascript
运行
复制
  int allocatedMem[26] = { 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 };
  int bitmapSize = 0;
    
  for (int i = 0; i < sizeof(allocatedMem) / sizeof(allocatedMem[0]); i +=8)
  {
    bitmapSize++;
  }

  cout << "Size of bitmap: " << bitmapSize << endl;
EN

回答 1

Stack Overflow用户

发布于 2022-03-14 17:38:30

在独立环境+malloc/free/断言中使用数据分配和初始化位图的一些代码:

代码语言:javascript
运行
复制
#include <cstddef>  // size_t
#include <cstdint>  // uint8_t
#include <climits>  // CHAR_BIT

#include <cassert>  // assert
#include <cstring>  // malloc / free
#include <iostream> // for output only
#include <bitset>   // for output only

// variable sized arrays in structs are only defined in C
extern "C" {
    struct Bitmap {
        using word = uint8_t;
        static constexpr size_t wordsize = sizeof(word) * CHAR_BIT;
        size_t size;
        word map[];
    };
}
using Bitmap = struct Bitmap;

// Cpp Core Guidelines violation:
// don't return ownership as pointer
// don't take array+size separately
Bitmap * alloc_init_bitmap(int allocated[], size_t n) {    
    // calculate bitmap size without risk of overflow
    const size_t bitmap_size = [](size_t n) {
        size_t t = n / Bitmap::wordsize;
        if (t * Bitmap::wordsize < n) ++t;
        return t;
    }(n);

    // allocate Bitmap
    const size_t total_size = offsetof(Bitmap, map) + bitmap_size;
    Bitmap *bitmap = (Bitmap*)malloc(total_size);
    assert(bitmap != nullptr);
    bitmap->size = n;

    // initialize bits
    Bitmap::word t = 0;
    Bitmap::word *pos = bitmap->map;
    Bitmap::word bit = 1;
    while (n-- > 0) {
        // add bit by bit from allocated to t
        t = t | bit * (*allocated++ != 0);
        bit <<= 1;
        if (bit == 0) {
            // all bits in t have been set, store word
            *pos++ = t;
            t = 0;
            bit = 1;
        }
    }
    // store partial word if allocatedMem isn't multiple of words
    if (bitmap->size % Bitmap::wordsize != 0) *pos = t;

    return bitmap;
}

int main() {
    int allocatedMem[26] = { 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 };
    Bitmap *bitmap = alloc_init_bitmap(allocatedMem, sizeof(allocatedMem) / sizeof(allocatedMem[0]));
    std::cout << "Bitmap of size " << bitmap->size << ":\n";
    for (size_t i = 0; i < bitmap->size / Bitmap::wordsize; ++i) {
        std::cout << " " << std::bitset<8>(bitmap->map[i]);
    }
    std::cout << std::endl;
    free(bitmap);
}

演示

使用std::bitset纯粹是为了输出映射。你必须用你自己获取位图内存的方式来替换malloc/free。如果位图是用来管理内存的,这是一个恶性循环。

输出看起来与allocatedMem不同,因为每个字都是输出,右边是0位,左边是7位。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71461131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档