How do I write a binary dump file of an array in my testbench

In reply to dave_59:

Ok, so with %u, I have the problem of every time I use it, I must write 4 bytes. This is a problem if I have 6 or 7 bytes to write from 1 array before other processes which can be integer aligned.

There is another issue leaving me a loss for words…
This 4 megabyte write:

for (int i=0;i<(2048*2048);i+=4)    $fwrite(fout_pointer,"%u",{bitmap[i+3],bitmap[i+2],bitmap[i+1],bitmap[i]}) ;

Took a total of 8 seconds, while this identical 4 megabyte write:

for (int i=0;i<(2048*2048);i+=4)    $fwrite(fout_pointer,"%s",{bitmap[i],bitmap[i+1],bitmap[i+2],bitmap[i+3]}) ;

was too quick to even measure the time it took to write. Only that all the 8’h00 have been replaced with 8’h20. If fact, if I were to abandon the 8’h00 for 8’h01 by doing this:

    for (int i=0;i<(2048*2048);i++)        begin
                                           if      (bitmap[i]==8'h00)  $fwrite(fout_pointer,"%s",8'h01) ; //
                                           else                        $fwrite(fout_pointer,"%s",bitmap[i]) ;  // 
                                           end

The 4 megabytes are also written almost instantly. Is there some reason why the %u is something like 20x slower than a %s?
Are there any other means of outputting a binary file, IE dumping the entire logic register bitmap[7:0][4meg]?