How to simple the code for the function to transfer 8 bits to 32bits

I have below code and I wonder if there is a simple way to implement it. (What I want is transfer the data in 8-bit array to the data with 32-bit array)


typedef byte unsigned uint8;
typedef int unsigned uint32;

function void 8to32(uint8 data_in[], ref uint32 data_out[]);
    
    int size = data_in.size;
    int left = size % 4;                        
    data_out = new[size/4+1];
    int j = 0;

    for (int i = 0; i < size; i = i + 4) begin
        data_out[j++] = {data_in[3+i], data_in[2+i], data_in[1+i], data_in[i]};
    end

// how to simple below code ??
    if (left == 1)
        data_out[j] = {24'b0, data_in[size-1]};
    else if (left == 2)
        data_out[j] = {16'b0, data_in[size-1], data_in[size-2]};
    else if (left == 3)
        data_out[j] = {8'b0, data_in[size-1], data_in[size-2], data_in[size-3]};

endfunction : 8to32


Thank you !

In reply to zz8318:
Your entire function can be simplified using the streaming pack operators

function void f8to32(uint8 data_in[], output uint32 data_out[]);
  data_out = {>>{data_in}};
  foreach(data_out[i]) data_out[i] = {<<8{data_out[i]}};
endfunction

Notes from your orignal example:

  1. An identifier cannot start with a number (8to32)
  2. Unless your function is automatic, the variable declarations are static and only get initialized once at time 0.
  3. The declaration of j cannot come after a procedural statement.

In reply to dave_59:

Thanks so much for your help.

The notes you mentioned is also fixed in my side before I posted this question. Thank you again!