Extract 3 3 bytes set from a byte array

hi can anyone help me how can i extract 3 3 bytes like payload[0]=00000000 payload[1]=00001010
and payload[3]=10101010 from a given array[100]={000000000000101010101010}.

i wrote a simple code which may be not correct, on output i got only 0’s please help me as soon as possible

i m attaching the sample.

frame.txt (1.35 KB)

byte array[3]; This only has 3 elements

array[i ]=arr[ i];

Not sure, I think you wanted: array[j]=arr[ i];

.

"array[j]=arr[ i]"

i did that but i m only getting 0,s as output

MODERATOR: do not use [i] as it is interpreted as italic command use [ i] instead (OK 2 spaces is sufficient ;)

Serial.write((byte)arr[ i]); Serial.println((byte)arr[ i]); You are loading array[] but printing arr[] Serial.write(array[j]); Serial.println(array[j]);

j++ never allows writing to array[0] Then you write to array[3] which is outside your array range. You only have array[0] array[1] and array[2]

To get help, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.

here is my complete sketch
<
#include <SoftwareSerial.h>

byte arr[100]={0000000000000000000000000000000100000011101101011000000111011011101100101011100101000110101000100000010100111101011100010111001000001110100101101011000101110110001101100010110101111010000111100111010001101010011000010001011011110011010011010100100000000000100101110100001000101001011100000000101101110101001001000001101001011101000010000111010101100011001000000000100010001101001010000111101101010111001000110110101100100010000100101000011011110100000101011000101110111111011001000001010100101001010100000100000001000101110111011000111100100011001010111111100111000010110111000101100101101110001001110000001001111000010001101111110111101000001010100011000011101001000010000110100110010010001010101001100100101010111010001101100111011001110100000110000110101101000010010101111001001101111010010110010001101000010011010100101100001000011011101101111000001110100101011001010001010101100101001111110001000100000001000000100110110101101010101000001001110001100001101011010101000000101001110000001111101001101000101100100010101000001110111110100001101010010100};
byte array[3];
int j=0;

void setup() {
Serial.begin(9600);

}

void loop() {
for(int i = 0; i<100; i++)
{

j++;
array[j]=arr*;*

  • if(j==2)*

  • {*

  • Serial.write((byte)array[j]);*

  • Serial.print((byte)array[j]);*

  • j=0;*

  • }*

  • }*
    }
    />
    *i have an array of binary data and i want to get 8bits from that data which will be held in array[0],array[1] and array[2] and this will be done repeatedly. i think now my explaination is clear to you. *

#include <SoftwareSerial.h>

byte arr[100]={0000000000000000000000000000000100000011101101011000000111011011101100101011100101000110101000100000010100111101011100010111001000001110100101101011000101110110001101100010110101111010000111100111010001101010011000010001011011110011010011010100100000000000100101110100001000101001011100000000101101110101001001000001101001011101000010000111010101100011001000000000100010001101001010000111101101010111001000110110101100100010000100101000011011110100000101011000101110111111011001000001010100101001010100000100000001000101110111011000111100100011001010111111100111000010110111000101100101101110001001110000001001111000010001101111110111101000001010100011000011101001000010000110100110010010001010101001100100101010111010001101100111011001110100000110000110101101000010010101111001001101111010010110010001101000010011010100101100001000011011101101111000001110100101011001010001010101100101001111110001000100000001000000100110110101101010101000001001110001100001101011010101000000101001110000001111101001101000101100100010101000001110111110100001101010010100};
byte array[3];
int j=0;



void setup() {
  Serial.begin(9600);
  

}

void loop() {
  for(int i = 0; i<100; i++)
  {
    
    j++;
    array[j]=arr[i];
  if(j==2)
  {
  
  Serial.write((byte)array[j]);
  Serial.print((byte)array[j]);
  j=0;
  }
  
  }

}

A 100 byte array requires 100 elements, separated by commas:

byte arr[100]={0,1,1,0,0,1,0,1, etc ...0,1,};

Your one long line doesn't seem correct to me.

@CrossRoads: You're doing better than I am. I don't even understand the question. Is this another X-Y question?

Also you do not need (byte)

Serial.write((byte)array[j]); Serial.print((byte)array[j]);

Serial.write((array[j]); Serial.print((array[j]);

j++ where it is will never let you load array[0]. Move it after the Serial.print. BTW, maybe you want Serial.println?

"Repeatedly"? You want to get 3 bytes, print those 3 bytes and then go back for the next 3 bytes? I expect this is just a component of a larger program but it looks like a good exercise to test your understanding of arrays.

I can see how you're incrementing j (the index on the smaller array) and when it gets to 2, you print the array and reset j. Good. But you just printed the last element in the array twice.

But then I look back at your declaration of the original array. You just give it a really long decimal number = something-something-Ten-Million, Ten Thousand and One Hundred. To have a hundred elements in the array, they have to be seperated by commas. To make them binary, the number must be prefixed by "0b" so the compiler knows those ones and zeros aren't normal base-ten numbers.

byte arr[100] = {0b0, 0b1001, 0b100111, .... (repeat for 100 items)   ... };

//Here's an easier way to get a lot of semi-random bytes into an array
char ar[100] = "This is my test data. It doesn't fill the full 100 bytes but I'm only testing right now.";

yes MorganS you got rite this will be a long program. Actually what i m doing is to capturing image from a camera and it is in binary form that i declared in array, then i have to make frames of 8 bytes having certain fields in it: starting byte(1byte), address(1byte),payload(3byte),crc(1byte),sequence no(1byte),end byte(1byte)

all these are in binary that i have to send serially now the payload field in it is the main problem for which i have to take 3 bytes from the original array.

i think now i m clear to u.

regarrds