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

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

问题描述

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

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

我正在使用:

#include <U8x8lib.h>

我确实知道uint8_t的行为类似于字节类型.

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'

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

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

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

我遇到了同样的错误:

initializer fails to determine size of 'hello'

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

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);

我不理解以下内容:

  1. uint8_t数组如何具有类似字符串的输入并可以正常工作,但是在涉及变量时却无法实现

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

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

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

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

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

在此先感谢您的帮助.

推荐答案

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

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

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

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

static uint8_t hello[] = "world";

本质上是

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

这也是普通的数组副本初始化,并且所需的大小是从值中自动推导出的,这就是为什么可以使用[]而不是[size]

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]

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

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

由于int的大小在编译时是已知的,因此您可以创建大小为int的数组,然后使用memcpy逐字节地将int值复制到该数组中:

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));

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

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

您需要事先知道String的长度,以便创建一个足以容纳String值的数组:

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天全站免登陆