Single digit inserted into a string of bytes (SOLVED)

This is probably very simple, ut I can’t figure it out :slight_smile:

I have a string of bytes that is sent over i2c to another Arduino:
byte test[8]={‘T’,‘e’,‘s’,‘t’,’ ',‘1’,‘2’,‘3’};

I can insert a digit or caracter
test[4]=‘Q’;
or
test[4]=‘6’;

But if I have a varible:
byte myVarible = 7;

How do I insert myVarible into the string?

I tried test[4]=myVarible
It compiles fine but the string sent over i2c is corrupt.

"string" has a fairly specific meaning in C - it is a character array, terminated with a zero (null) character.
You don't have that, and you don't have room in your eight element array for a null character.

test [4] = myVarible + '0'; may be what you're looking for.

byte myVarible = 7;

Big difference between 7 and '7'. Which one do you want to add?

AWOL:
"string" has a fairly specific meaning in C - it is a character array, terminated with a zero (null) character.
You don't have that, and you don't have room in your eight element array for a null character.

Come on now, a string is an ordered set of bytes. I send/receive them using the serial monitor all the time. A c-string is probably what you are thinking of, which has a null attached to the end of the original string of bytes.

I've never heard of a "c-string".
Maybe a "G-string".

"string" has a fairly specific meaning in C (my quote, my emphasis)

a string is an ordered set of bytes

You're going to have to explain to me what is ordered about byte test[8]={'T','e','s','t',' ','1','2','3'};, other than the indices.

Edit: I just googled "c-string", but I suggest you don't if you're at work.

AWOL:
I've never heard of a "c-string".
Maybe a "G-string".

"string" has a fairly specific meaning in C (my quote, my emphasis)

a string is an ordered set of bytes

You're going to have to explain to me what is ordered about byte test[8]={'T','e','s','t',' ','1','2','3'};, other than the indices.

You have a concept of what a "string" is, but it is somewhat technically sloppy when applying your concept to byte sets that are not null terminated (especially when you seem to lecture others on the subject). It also appears to me (but apparently not to you) that there is a desired ordering of the bytes in the string 'T','e','s','t',' ','1','2','3' presented by the op. I'm not familiar with i2c, but it appears to me to be a serial based data transmission method in which even random bytes will be sent in an ordered fashion. I've seen strings that are null terminated with a kite.

I've done some programming in different languages, but not much Arduino.
OK, maybe I have a string, maybe I don't (I never wear it though)
I guess I have an array of bytes.

What I do is send up to 8 bytes from several slave arduinos to a master arduino.
A slave can send "T=26.5" or "T=112.25"
Another slave can send "Left" or "Right"
A third can send "35" or "Empty"

In other words, a mix of numbers and letters.
The master receives and print this information on a LCD and saves it to a SD card.

Everything is working except when the numbers are a variable.

Everything is working except when the numbers are a variable.

Well, the byte array is a variable too, so it must be the format of the variable, as pointed-out earlier.
The number 7 is not the same as the character '7'.
The former has the hex value 0x07, the latter 0x37, or '0' + 7.

Figured it out :slight_smile:
Thanks for your help getting me on the right track.
What I want is the number 7 to be readable so to speak.
If i want to send "Left 7"
And the 7 is a variable called testp:
I can write

test[0]='L';
test[1]='e';
test[2]='f';
test[3]='t';
test[4]=' ';
test[5]=testp + 48;

The "+ 48" since the number 7 is DEC 55 in ASCII. (7 + 48 = 55).

Thanks again!

test[5]=testp + 48;

Which is why I wrote test[5]=testp + '0'; - it’s easier to read and to see exactly what you’re doing.

Ahhh, of course, now I understand what '0' + 7 means.
Will change my code.
Thanks :slight_smile: