converting a bool array into byte array

hello

i have a large array containing (0,1) values, like this:

static const uint8_t image_data_1[9000] = {
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1
.........
}

i need to divide each line by 8, representing a byte and store them in an array as below:

static const unsigned char PROGMEM arduino_logo[] ={
    B00000000, B00000000, B00000111, B11111111,
    B11111111, B11100000, B00000000, B00000000,
    B00000000, B00000000, B01111111, B11111111, 
    B11111111, B11111110, B00000000, B00000000,
    ... 
};

thanks in advanced.

Bool is a byte variable, but can only conteins 0 or 1. You can cast it and assign the value you want

bool a=0;//a=0
a=5;//a=1
byte (a)=5;//a is casted byte and it is =5
a=1; //a=1, but byte

I'm not compketly sure about it, but I think it is like this

i have a large array containing (0,1) values

Where are these values coming from and do they need to be put in an intermediate array ? Could you not put them directly into the target array ?

What type of Arduino are you using ?

On Uno you can't have 9000 bools. The only thing you can do is to transform than into bytes and put in a byte 8 of than (so you pass from 9000 to 1125 bytes, that are a lot, but less than before)

Google "C++ bit-shifting operator tutorial".

There is no (easy) method of writing to program (flash) memory after compilation do the concept of your static byte array in PROGMEM doesn't exist unless you want to do the conversions before. Also, as has been mentioned, a boolean value consumes 1 byte so 9000 of them will need about the same number of bytes, not available on the UNO or Mega.

You can define the boolean array to be stored in PROGMEM and consume 9K of flash, then convert to a byte array with a quick function in setup:

byte    byte_array[sizeof(binary_array) >> 3] = {};

for (uint16_t i = 0; i < sizeof(byte_array); i++)
{
    for (byte j = 0; j < 8; j++) 
    {
        byte_array[i] <<= 1;
        byte_array[i] |= (binary_array[((i << 3) + j)]);
    }
    bin4ln(byte_array[i]);
}

As has also been mentioned, this will consume now a whole bunch of precious RAM. Since you've already got the data in PROGMEM, why not think about just extracting the data as needed. The above function can be modified to replace the outer loop (the one controlled by i) with a value that represents the base index in the binary array for the extraction of 8 consecutive values into a byte.

You can define the boolean array to be stored in PROGMEM

If you are going to do that then you might just as well convert the boolean array to an array of bytes as a one off process and store that instead.

We don't have the full story so maybe that is what the OP is planning to do. I look forward to finding out where the boolean array data comes from.

In a text editor, start by replacing all occurrences of ", " with "" to pack all of the digits together:

static const uint8_t image_data_1[9000] = {
    11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100000011100000011100000011100000011100011
.........
}

Then use the regular expression "([01][01][01][01][01][01][01][01])" to find groups of 8 binary digits and the replacement text of "B\1, " to put 'B' in front and ", " after the group. Replace all groups.

static const uint8_t image_data_1[9000] = {
    B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111110, B00000111, B00000011, B10000001, B11000000, B11100011, 
.........
}

Delete the last comma.

I would then take the extra step of manually inserting newlines and spaces to improve the format:

static const uint8_t image_data_1[9000] = {
  B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, 
  B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, 
  B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, 
  B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, 
  B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, 
  B11111111, B11111111, B11111111, B11111110, B00000111, B00000011, B10000001, B11000000, 
  B11100011
.........
}

If, for some reason, you need to access each bit independently of the others, and you aren't familiar with bit-shift, bit mask, and other bit manipulation, you can set up a struct or several structs and group them according to similar use or other logical groupings.

PaulMurrayCbr:
Google "C++ bit-shifting operator tutorial".

A really good bit-manipulation tutorial that was eventually polished and published in Circuit Cellar.

johnwasser:
In a text editor, start by replacing all occurrences of ", " with "" to pack all of the digits together:

Then use the regular expression "([01][01][01][01][01][01][01][01])" to find groups of 8 binary digits and the replacement text of "B\1, " to put 'B' in front and ", " after the group. Replace all groups.

Delete the last comma.

Nice. One of those things that's obvious the moment it's stated: this person simply has a problem with the text of their program. Of course, [01]{8} also works if your editor does extended regexes.

There's probably also a way to turn this into a binary and to tell the linker "treat that binary file over there as an array of byte named foo".

Ah ha! Here we go: FAQ.html#faq_binarydata

You'd then declare something like

extern __PROGMEM__ char _binary_foo_bin_start[];
extern __PROGMEM__ char  _binary_foo_bin_end[];

to get the stuff out of progmem.

for(char *p = _binary_foo_bin_start; p < _binary_foo_bin_end; p++) {
  char ch = readProgmemByte(p); // not sure what the actual name of the function is
}

[/code]

Silente:
Bool is a byte variable, but can only conteins 0 or 1. You can cast it and assign the value you want

Bool bool is a bit variable (1-bit data type)? It can hold only 1-bit data. The sample codes are:

bool n;
byte x = 0b10101010;
n = bitRead(x, 1);
Serial.print(n, BIN); //shows: 1

GolamMostafa:
Bool bool is a bit variable (1-bit data type)? It can hold only 1-bit data. The sample codes are:

If you can't address it individually, it can't be a data type.

AWOL:
If you can't address it individually, it can't be a data type.

In 8051 architecture, there are bit addressable RAM locations which could be accessed as a single bit through the execution of this kind of instruction: MOV CY, bitAddress. This single bit is a part of another 8-bit chunk for which there is an address.

In ATmega328P/ATmega32A, I have not seen such bit addressable RAM locations. The Arduino supports bool as a data type which I have conceived from the execution of the following code:

bool n = bitRead(x, 6);

In the above code, 1-bit data is being transferred/copied from bit position 6 of the source (x) to 1-bit destination (n). Am I not addressing n as a single bit location?

In Arduino (my understanding), the keyword bool somehow simulates a bit addressable location in RAM memory of ATmega. However, there are bit addressable instructions in ATmega -- sbic, sbis, sbi, and the like.

In Arduino (my understanding), the keyword bool somehow simulates a bit addressable location in RAM memory of ATmega

In C, if you can't take its address, it can't be a datatype.
In C, on an AVR, you can't get the address of a bit.
bool is not a single bit - your understanding is flawed.

AWOL:
bool is not a single bit - your understanding is flawed.

If we accept that bool has come from boolean, then bool can have only one state at a time -- either 0 or 1.

In Arduino, the bool has nothing (my understanding -- conceived now) to do with boolean; it has the established definition as given in the Arduino Reference Manual. true can have any value from 0x01 - 0xFF; false could have a value of 0x00.

Thanks with p...

Everybody is right. A bool variable is a byte of memory. The byte will be either 0 (false) or non-0 (true) and when converted to an integer will be 0 (false) or 1 (true).

johnwasser:
Everybody is right. A bool variable is a byte of memory. The byte will be either 0 (false) or non-0 (true) and when converted to an integer will be 0 (false) or 1 (true).

How can everybody be right when the bool is a keyword, and it has a pre-defined meaning in the context of C Programming Language? It refers to a variable (a memory location with a symbolic name) which holds 8-bit data.

thank you so much johnwasser for your help, so i will clarify the case, i have a 0.96" OLED, and i want to display an image, so i used a lcd converter software to convert the image into hex, so i got something like this:

static const uint8_t image_data_1[] = {
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
.........

i dont know if this is a bool data, but i noticed that it it can be represented with values of 1 for 0xff and 0 0x00, and i used word to change the data, the library demo uses this format (B01001011) to display a logo, and it works fine with my display, so i did as johnwasser advised, but im not familiar with regular expression, can i use microsoft word, or should i use some programming language,
i really appreciate your help, thank you

In the C programming language, a bool value is one bit - true or false.

As I understand it: 'true' does not have the value '1', it has the value 'true'. But the expression (int) true always evaluates to 1. Of course, the easiest way to do this is for the compiler to actually use 1 as the in-memory representation, and that's almost certainly what it does. Likewise, when an int value is downcast to bool, '0' means false and anything else means true.

Try this:

void setup() {
  Serial.begin(9600);
  int a = 5;
  Serial.print("a is "); Serial.println(a);

  bool b = a;
  Serial.print("b is "); Serial.println(b);

  int c = b;
  Serial.print("c is "); Serial.println(c);
}

void loop() {
}

When C is compiled into code that can be executed on an arduino in particular, the compiler uses an entire byte of memory for it - 8 bits. If the 8051 does indeed have certain addresses that are single-bit values, then I suppose the compiler would be free to use them as such. Only for static variables, of course, unless the stack had a very weird structure indeed.

The distinction between the abstract one-bit 'bool' type and its concrete representation is a bit like the distinction between a floating point type and its concrete representation. There's no rule stipulating that it must be represented as an IEEE 16-bit float. But it's what people do: why would you bother doing it differently? Likewise, a compiler is free to represent the true and false values however it likes, provided that when it is promoted to an integer value, true becomes a numeric 1 and false becomes a numeric 0.

tl;dr: people on this thread are just splitting hairs over what words mean.