Declaring char arrays

The Particle reference states “The char datatype is a signed type, meaning that it encodes numbers from -128 to 127.”, which is OK for my application.
I intend to use char arrays and the array that I’ve declared doesn’t show any negative values at all; only the values between 0 and 127 are correctly shown;
For instance the value -100 is shown as 156 (so: 256 minus 100).

Do I have to do something special in declaring such a signed array?
(The declaration I use is nothing more than: char DATATB[622]:wink:

When you don’t want to store characters but numeric values you should probably best opt for a numeric data type like int8_t (signed 8bit integer) or uint8_t (unsigend 8bit integer) or even byte.

1 Like

Hi ScruffR,
I want to use character arrays. How do I convert from a series of signed 8bit integers (int8_t) to a character array?

Then why do you care about the numeric values? Characters are unsigned or what would be the negative of A?

In memory an int8_t array is indistinguishable from a char array - so no need for conversion.
If you need to feed a "mistyped" array into a function that requires a different (but bit compatible) type you just use a type cast.

I understand that an int8_t array and a char array are indistinguishable in memory.
What I do not understand is what has been stated in the Particle Docs: “The char datatype is a signed type, meaning that it encodes numbers from -128 to 127.” because a char array that I have declared char DATATB[622] seems not to accept any negative numbers, only postive numbers.
So therefor my question is: should I do something additional to just declaring the array as I did.
Or how should I understand the Particle Docs?

Maybe you can prefix the char with signed, which is signed char DATATB[622]

The char data type is signed. However, depending on how you view it, it may appear as its unsigned equivalent. For example, if you use sprintf() with %u instead of %d.

However, given your declaration of an array of 622 characters, I’m guessing you’re using this for a publish. In this case, the rules of publish take precedence over the char data type. Publish only takes a null-terminated Unicode UTF-8 string, not an array of bytes. There is no equivalent of negative numbers in Unicode UTF-8.

If you are encoding binary data you must use some other encoding scheme like Base64 or Base85 that encodes binary data (including negative signed 8-bit values) in ASCII.

3 Likes