Array of ints to array of bytes (using streaming operator)

I need to convert a dynamic array of ints to a dynamic array of bytes, in the right order:

The first byte should be the least significant bits of the first integer. I wanted to do this using the streaming operator, but this seems to reverse either the int order or the byte-in-int order:

        int unsigned int_data[]=new[3];
        byte unsigned byte_data[];
        int_data[0]=0;
        int_data[1]=1;
        int_data[2]=2;
        byte_data={<<byte{int_data}};
        foreach(byte_data[z]) begin
            $display("  %0d",byte_data[z]);
        end

This prints:

2

0

0

0

1

0

0

0

0

0

0

0

So the order of the ints is wrong. I really need it to be:

0
0
0
0
1
0
0
0
2
0
0
0

Using >> gets the int order right, but the byte order wrong.

I tried some things using the ‘with’ extension, but can’t figure out how to use it.

In reply to Nico75:

How about doing reverse() on the array prior to streaming? Try:



module m;
  
  int unsigned int_data[]=new[3];
        byte unsigned byte_data[];
  
  initial begin : b1
        int_data[0]=0;
        int_data[1]=1;
        int_data[2]=2;
        byte_data={<<byte{int_data}};
        foreach(byte_data[z]) begin
            $display("  %0d",byte_data[z]);
        end
    
    int_data.reverse();
    byte_data={<<byte{int_data}};
        foreach(byte_data[z]) begin
            $display("  %0d",byte_data[z]);
        end
  end : b1
endmodule

Regards
Srini
www.verifworks.com

In reply to Srini @ CVCblr.com:

Good suggestion, that will definitely work. Just a little bit concerned about the efficiency.

In reply to Nico75:
You can do this in one line, but I don’t know how much more efficient it will be.

byte_data={<<byte{{<<int{int_data}}}};

You might also try

byte_data = new[4*int_data.size()];
foreach(int_data[z]) 
    byte_data[z*4 +:4] = {<<byte{int_data[z]}};