Read write a structure byte by byte

Hello, I would like to read (or write) a structure of 32 bytes using a table 'byte tb[32]'. someone can help me, please. Thank you. jihelb85

You have an array named tb which can hold 32 bytes. The array index will be between 0 and 31, so to write to the first element, for example, you do this

tb[0] = value;

to write to the last element

tb[31] = value;

Can you please explain what you want to do in more detail

Post your program so we can see the context.

If you mean that you want to work through a struct byte by byte then a simple way is to use a union to map a byte array to the same memory space as the struct.

...R

typedef struct {
unsigned long ulong1;
unsigned long ulong2;
unsigned long ulong3;
unsigned long ulong4;
unsigned long ulong5;
unsigned long ulong6;
byte tbbyte[8];
}MyStructure;

MyStructure mst;

void setup{
//— init mst—
for(byte i=0; i < sizeof(MyStructure ); i++){
//Here, fill the mst byte with random(256)

  • }*
    }

exactly Robin2 I know how to do it in Delphi but not in the Ide Arduino. Thank you

typedef struct
{
  unsigned long ulong1;
  unsigned long ulong2;
  unsigned long ulong3;
  unsigned long ulong4;
  unsigned long ulong5;
  unsigned long ulong6;
  byte tbbyte[8];
} MyStructure;

MyStructure mst;

void setup()
{
  Serial.begin(115200);
  for (byte i = 0; i < sizeof(mst.tbbyte); i++)
  {
    mst.tbbyte[i] = random(256);
  }

  for (byte i = 0; i < sizeof(mst.tbbyte); i++)
  {
    Serial.println(mst.tbbyte[i]);
  }
}

void loop()
{
}

Note that as written this will always produce the same sequence of “random” numbers.

Thanks UKHeliBob, but it is the totality of the bytes of the structure (and not only tbbyte[]) that I want to initialize (1 byte at a time) using a loop 'for' (as for a table)

jihelb85: I know how to do it in Delphi but not in the Ide Arduino.

Google cplusplus union

...R

Do you want every byte of the struct, including those that make up the unsigned longs, to be set to a random value ?

As a matter of interest, why aren't the unsigned longs in an array ?

Thank you for your help.
The example below does the job properly.
I wish it would be useful for others.
///////////////////////////////////////////////
typedef struct {
unsigned long ulong1;
unsigned long ulong2;
unsigned long ulong3;
unsigned long ulong4;
unsigned long ulong5;
unsigned long ulong6;
byte tbbyte[8];
}MyStructure;

union job {
MyStructure umst;
byte utb[32];
}job1;

void setup{
//— init mst—
for(byte i=0; i < sizeof(MyStructure ); i++){
job1.utb = random(256);

  • }*
    }
    /////////////////////////////////////////

It would help considerably if you used code tags around your code to avoid nonsense lines like this appearing

job1.utb = random(256);
which I would guess is not what your code actually looks like.

Cast the struct to a byte pointer and fill the 32 bytes in a for loop.

jihelb85:
Thank you for your help.
The example below does the job properly.
I wish it would be useful for others.

it could be done with pointers as well…

struct SomeStruct{
  unsigned long ulong1;
  unsigned long ulong2;
  unsigned long ulong3;
  unsigned long ulong4;
  unsigned long ulong5;
  unsigned long ulong6;
  byte tbbyte[8];
}myStructure;

void setup() 
{
  uint8_t* ptr = (uint8_t*)&myStructure;
  for (size_t i = 0; i < sizeof(SomeStruct); i++)
  {
    (*(ptr + i)) = random(256);
  }
}

as just mentioned:

sterretje:
Cast the struct to a byte pointer and fill the 32 bytes in a for loop.

Or using array notation:

  for (size_t i = 0; i < sizeof(myStructure); i++)
  {
    ((uint8_t*) &myStructure)[i] = random(256);
  }

Pieter

EXCELLENT BulldogLowell and PieterP

That's exactly what I was looking for! This opens horizons for me. A big thanks to you 2.

jihelb85: EXCELLENT BulldogLowell and PieterP

That's exactly what I was looking for! This opens horizons for me. A big thanks to you 2.

Great,

and notice you don't need that typedef stuff. This is C++; a struct/union are automatically typedef'ed.