1

I bumped into byte datatype recently. My knowledge of C/C++ is shallow, but I know char and I couldn't find much on byte in C++ docs. I looked it up in the Arduino docs:

It’s recommended to only use char for storing characters. For an unsigned, one-byte (8 bit) data type, use the byte data type.

Is byte datatype a solely Arduino construct? If so, why is it recommended over char? Both are 8-bit so why would it make any difference?

asked Mar 3, 2020 at 10:58
2
  • 1
    char is configured as signed in compiler setting of Arduino Arduino cores Commented Mar 3, 2020 at 11:04
  • 2
    Whenever I make something bigger than just 50 lines, I always use the most specific types like: uint8_t, int8_t, uint16_t, int16_t, uint32_t and int32_t to know exactly what is signed or unsigned and if it is 8, 16 or 32 bits. And I use char for characters, and bool for booleans. In this case you never have to think what fits (or should fit) in a variable. Commented Mar 3, 2020 at 11:23

3 Answers 3

3

Is byte datatype a solely Arduino construct?

Yes. Much of the Arduino "language" is "bent" to look like Java, since it originated from Processing, which is Java. So we have data types the "look like" Java ones, including "byte" for "unsigned char" and "boolean" for "bool".

If so, why is it recommended over char?

Personally I don't ever recommend using it, since it's non-standard. Instead you should use uint8_t which is essentially the same thing, but portable.

It is recommended to use "hard signedness" data types for portability. The problem with char is that whether it is signed or not is entirely down to the compiler implementation. Some systems have it as signed, some as unsigned. By using byte (or better, uint8_t) you are always going to get an unsigned 8-bit value.

For just storing of binary data it doesn't really make any difference, but when it comes to doing mathematics with the stored data you may find you suddenly have negative values when you don't want negative values (a char might be storing your number as -128 to +127, not 0 to 255).

answered Mar 3, 2020 at 11:04
1
  • Great explanation. I will stick to specific unit8_t in the future and will go back and refactor my "chars". I prefer to stick to C/C++ syntax than to wander into Java and have another language to be shallow at. Commented Mar 3, 2020 at 11:50
1

Is byte datatype a solely Arduino construct?

Yes. Much of the Arduino "language" is "bent" to look like Java, ...

While the rest of the answer is of merit; No, byte is a fundamental unit of storage at the hardware level. The word was coined in the 1950s at IBM and defines a collection of bits. You will find byte defined in many programming languages, it is not just an Arduino, or Java, thing. You will, for example, find it in C++ when

  • working with some hardware interfaces (dis-allowing implicit conversions to other types, which prevents accidental misuse and improves code safety);

  • using bitwise operations (convenient for low-level manipulations);

  • or representing raw data memory (std::byte conveys that you are working with raw bytes rather than characters or integers).

Still, the suggestions by Majenko and DataFiddler are valid with Arduino.

answered Dec 20, 2024 at 16:39
0
0

Just a little add-on to Majenko's answer:

If a function has a char* parameter, it's usually expecting a zero-terminated text.

If a function has a byte* parameter, it's usually any binary data, eventually containing '0円' bytes. The buffer size needs to be an additional parameter in this case.

If there's nothing else to consider, why not use that for your variable definition too: Use char only if it contains an 8bit character. Or something like char text[]="hello"; For binary data it might be relevant if it's signed (int8_t) or unsigned (uint8_t).


"usually" implies of course that you should read the doc.

answered Mar 3, 2020 at 12:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.