静态 uint8_t 数组的输入过程和类型 [英] Input process and type for static uint8_t array

查看:47
本文介绍了静态 uint8_t 数组的输入过程和类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试在 Arduino IDE 中将整数变量转换为静态 uint8_t 数组的值.

我正在使用:

#include 

而且我确实理解 uint8_t 的作用类似于字节类型.

目前,数组有一个设定值:

static uint8_t hello[] = "world";

从我的角度来看,world"看起来像一个字符串,所以我想我会从创建一个字符串变量开始:

String world = "world";静态 uint8_t hello[] = 世界;

这不起作用并给了我错误:

初始化程序无法确定hello"的大小

如果我也这样做,而是将world"更改为如下所示的 int...

int world = 1;静态 uint8_t hello[] = 世界;

我得到了同样的错误:

初始化程序无法确定hello"的大小

我已经通过以下过程成功地将 uint8_t 数组转换为字符串:

static uint8_t hello[] = "world";String helloconverted = String((const char*)hello);

我不明白以下内容:

  1. uint8_t 数组如何具有类似字符串的输入并正常工作,但在涉及变量时则不然

  2. 如何创建一个字符串变量作为 uint8_t 数组的输入

  3. 如何创建一个 int 变量作为 uint8_t 数组的输入

预先感谢您的帮助.

解决方案

uint8_t 数组如何可以有类似字符串的输入并且工作正常,但是不涉及变量

字符串文字本质上是一个以 null 结尾的字符数组.所以

static uint8_t hello[] = "world";

本质上是

static uint8_t hello[] = {'w','o','r','l','d','\0'};

这也是一个普通的数组拷贝初始化,需要的大小是从值中自动推导出来的,这就是为什么你可以使用[]而不是[size]

<块引用>

如何创建一个 int 变量作为 uint8_t 数组的输入

由于 int 的大小在编译时已知,您可以创建一个 int 大小的数组并将 int 值复制到它的字节带有 memcpy 的字节:

int world = 1;静态 uint8_t hello[sizeof(world)];memcpy(你好,&world,sizeof(你好));

<块引用>

如何创建一个字符串变量作为 uint8_t 数组的输入

您需要事先知道 String 的长度,以便创建一个足够大的数组以适合 String 值:

String world = "Hello";//5 个字符静态 uint8_t 你好[5];world.toCharArray((char *)hello, sizeof(hello));

根据您的需要,您可能还想处理终止 null.

I am currently trying to convert an integer variable to the value of a static uint8_t array in the Arduino IDE.

I am using:

#include <U8x8lib.h>

And I do understand that uint8_t acts similarly to the byte type.

Currently, the array has a set value:

static uint8_t hello[] = "world";

From my perspective, "world" looks like a string, so I thought I would start with creating a string variable:

String world = "world";
static uint8_t hello[] = world;

This did not work and gave me the error:

initializer fails to determine size of 'hello'

If I do the same, but instead change "world" to an int like below...

int world = 1;
static uint8_t hello[] = world;

I get the same error being:

initializer fails to determine size of 'hello'

I have been successful in converting the uint8_t array to a string through the following process:

static uint8_t hello[] = "world";
String helloconverted = String((const char*)hello);

I don't understand the following:

  1. How a uint8_t array can have a string-like input and work fine, but not when a variable is involved

  2. How to create a string variable as the input for the uint8_t array

  3. How to create a int variable as the input for the uint8_t array

Thanks in advance for your assistance.

解决方案

How a uint8_t array can have a string-like input and work fine, but not when a variable is involved

String literal is essentially an array of chars with terminating null. So

static uint8_t hello[] = "world";

Is essentially

static uint8_t hello[] = {'w','o','r','l','d','\0'};

Which is also a normal array copy initialization, and the size needed is auto-deduced from the value, this is why you can use [] and not [size]

How to create a int variable as the input for the uint8_t array

Since size of int is known at compile time you could create an array of size of int and copy int value to it byte by byte with memcpy:

int world = 1;
static uint8_t hello[sizeof(world)];
memcpy(hello, &world, sizeof(hello));

How to create a string variable as the input for the uint8_t array

You need to know the length of the String beforehand so you can create an array big enough to fit the String value:

String world = "Hello"; // 5 chars
static uint8_t hello[5];
world.toCharArray((char *)hello, sizeof(hello));

Depending on what you need, you might want to also handle terminating null.

这篇关于静态 uint8_t 数组的输入过程和类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆