1

I'm writing out data using BinaryWriter.
Now I'm writing out a string that will be padded to 256 bytes, with null bytes to make up for the leftover space (eg: string "hello world" takes up 11 bytes, so I will need to write another 245 null bytes).

The current approach is to just write out the string normally, calculate the length of the string and subtract that from 256 to get the desired number of null bytes, and then use a for loop to write out all of those nulls.

But maybe it's better to first build my null-padded string and then write it out all at once. How can I pad my string with null bytes to a length of n?

I guess they would be null chars rather than null bytes.

MxLDevs
  • 19,048
  • 36
  • 123
  • 194

1 Answers1

5

Try using the PadRight function:

string result = "Hello World".PadRight(256, '\0');
// TODO: write the resulting string using the binary writer
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928