39041

Input process and type for static uint8_t array

Question:

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:

<ol><li>

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

</li> <li>

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

</li> <li>

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

</li> </ol>

Thanks in advance for your assistance.

Answer1:

<blockquote>

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

</blockquote>

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]

<blockquote>

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

</blockquote>

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

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

</blockquote>

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.

Recommend

  • Android Maps-API v2: where are the Overlays?
  • Gnuplot histogram 3d
  • Cache-control: Is it possible to ignore query parameters when validating the cache?
  • Using Recaptcha with EPiServer XForms
  • Optimal way to set pixel data?
  • How to convert list into string with quotes in python
  • How can integers (keys & values) be added and retrieved from a NSDictionary
  • JSON data through JS/AJAX into PHP
  • calculating number of bytes of each row in an image
  • Swift Initialization Rule Confusion
  • How can I tell a form not to dispose a particular control when it closes?
  • Hash Code in SQL Server?
  • Intel-64 and ia32 atomic operations acquire-release semantics and GCC 5+
  • Read text file that is not in the main package in a runnable jar
  • Spring Cloud Microservice Architecture Confusion
  • Initializer list vs. initialization method
  • Control modification in presentation layer
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Fetching methods from BroadcastReceiver to update UI
  • Counter field in MS Access, how to generate?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • R: gsub and capture
  • AT Commands to Send SMS not working in Windows 8.1
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • python regex in pyparsing
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • Linking SubReports Without LinkChild/LinkMaster
  • apache spark aggregate function using min value
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Sorting a 2D array using the second column C++
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How to load view controller without button in storyboard?