Changing specific character in a byte

Hi, I have come to wonder whether it is possible to change a specific character from a byte

My byte is part of a byte array:

byte f00[8] = {
    0b00000,
    0b00000,
    0b00000,
    0b00000,
    0b00000,
    0b00000,
    0b00000,
    0b00000
};

I have then picked no. 0 in this array:

f00[0]

And then I want to change the 5'th character in this first byte in the array.

I think it would be pretty easy to do if I could convert this byte to a String, and then use things like
split to change the specific char, and then change it back to a byte (which it btw has to be, I can't change that).

But I can't seem to figure out how to do this.

Your array holds 8 bytes
Each byte could hold a single character so you can't

You could change the 5th bit in the first byte of the array. Do you understand the difference between a bit and a byte ?

If it means to change the 4th bit, use bitwise XOR.

e.g.

f00[0] ^= 1 << 4;

This swaps the bit.
If it is 0, it is set to 1.
If it is 1, it is set to 0.

Although 4 is specified, because the rightmost bit is the 0th bit.
So it is counted as 4 3 2 1 0 instead of 5 4 3 2 1.

1 Like

Or use the bitRead() and bitWrite() functions if you are not comfortable with bit masking and shifting

byte bitValue = bitRead(f00[0], 4);
bitWrite(f00[0], 4, !bitValue);
2 Likes

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.