简体   繁体   English

序列化/反序列化未签名的字符

[英]Serialize/deserialize unsigned char

I'm working on an API for an embedded device, and need to display an image generated (by the API). 我正在为嵌入式设备使用API​​,并且需要显示(通过API)生成的图像。 The screen attached to the device allows me to render bitmaps, with data stored as unsigned char image[] = { 0B00000000, 0B00001111, 0B11111110... } . 设备上的屏幕允许我渲染位图,数据存储为unsigned char image[] = { 0B00000000, 0B00001111, 0B11111110... }

What is the easiest way to deserialize a string in whatever format needed? 以任何所需格式反序列化字符串的最简单方法是什么?

My approach was to create a stringstream , separate by comma and push to vector<char> . 我的方法是创建一个用逗号分隔的stringstream ,并推送到vector<char> However, the function to render bitmaps will only accept char , and from what I can find online it seems to be quite difficult to convert it. 但是,渲染位图的功能将仅接受char ,从我在网上可以找到的内容来看,转换它似乎非常困难。 Ideally, I'd rather not use a vector at all, as including it adds several kbs to the project, which is limited in size by both the download speed of the embedded device (firmware is transferred by EDGE) and the onboard storage. 理想情况下,我宁愿完全不使用vector ,因为包括它会向项目增加几kb,而其大小受嵌入式设备的下载速度(固件由EDGE传输)和板载存储的限制。

From the comments, it sounds like you want to convert a string composed of a series of "0b00000000" style literals, comma separated, into an array of their actual values. 从注释中看来,您好像要将由逗号分隔的一系列“ 0b00000000”样式文字组成的字符串转换为它们的实际值数组。 The way I would do this is to: 我这样做的方法是:

  1. Get the number of bytes in the image (I assume this is known from the string length?). 获取图像中的字节数(我假设这是从字符串长度得知的吗?)。
  2. Create a std::vector of unsigned char to hold the results. 创建一个unsigned charstd::vector来保存结果。
  3. For each byte in the input, construct a std::bitset from the string value, and then get its actual value. 对于输入中的每个字节,从字符串值构造一个std::bitset ,然后获取其实际值。

Here's a code example. 这是一个代码示例。 Since you have said you'd rather not use vector I have used C-style arrays and strings: 既然您已经说过,您宁愿不使用vector我也使用了C样式的数组和字符串:

#include <bitset>
#include <cstring>
#include <iostream>
#include <memory>

int main() {
    auto input = "0b00000000,0b00001111,0b11111111";
    auto length = strlen(input);

    // Get the number of bytes from the string length. Each byte takes 10 chars
    // plus a comma separator.
    int size = (length + 1) / 11;

    // Allocate memory to hold the result.
    std::unique_ptr<unsigned char[]> bytes(new unsigned char[size]);

    // Populate each byte individually.
    for (int i = 0; i < size; ++i) {
        // Create the bitset. The stride is 11, and skip the first 2 characters
        // to skip the 0b prefix.
        std::bitset<8> bitset(input + 2 + i * 11, 8);

        // Store the resulting byte.
        bytes[i] = bitset.to_ulong();
    }

    // Now loop back over each byte, and output it to confirm the result.
    for (int i = 0; i < size; ++i) {
        std::cout << "0b" << std::bitset<8>(bytes[i]) << std::endl;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM