Converting parts of array into number and other way around

I want to create a program, where I could convert a decimal number to binary number and fill the array with 0 and 1.
For instance:
byte array={0, 0, 0, 0, 0, 1};
Now I would like to change first 3 values in array with decimal number 3 which is 011 in binary. so the array would look now
byte array={0, 1, 1, 0, 0, 1};

is it possible?
Also how would I get decimal back from that array? (for instance I would like to get last 3 values from array ({0, 0, 1}) and convert them to decimal, which would then be 1.)

Thank you for answers :slight_smile:

Hello,

Yes it's possible but, are you sure that you need to waste 8x more memory than necessary?

You don't need to convert a number into binary. You can show that number as binary notation, it will still be the same number.

You can achieve what you want without an array, by using function bitWrite.

And if you really want to use an array, you will also need a for loop and function bitRead.

This

byte array[]={0, 0, 0, 0, 0, 1};

is not a number. It is 6 separate numbers each of which could range between 0 and 255. And this

byte array[]={0, 1, 1, 0, 0, 1};

is also 6 numbers, some of which happen to be different.

To change the number in the 2nd element from 0 to 1 you just need

array[1] = 1; // note that elements count from 0

I reckon we could give much better advice if you tell us what you are trying to achieve. A completely different approach may be simpler.

...R