Padding buffer

  • Thread starter Thread starter Neil W.
  • Start date Start date
N

Neil W.

I need to pass a string to a Windows API, but I need to pad the buffer with
binary zeros up to a specific length.

For example, if the string is "mystring", I need to pad it with binary zeros
up to 64 bytes. How do I do that in C#?

Thanks.
 
more than one way to skin the cat but here is one:

string val = new string(new char[]{'0'}).PadLeft(64, '0');
System.Diagnostics.Debugger.Break();
 
Neil said:
I need to pass a string to a Windows API, but I need to pad the buffer with
binary zeros up to a specific length.

For example, if the string is "mystring", I need to pad it with binary zeros
up to 64 bytes. How do I do that in C#?

Try:

string s = "mystring";
string padded = s.PadRight(64, '\u0000');

Arne
 
more than one way to skin the cat but here is one:

string val = new string(new char[]{'0'}).PadLeft(64, '0');

I believe you'd want '\0' and not '0' because the second one is for the
character "0" which is ASCII 48 and the poster wants ASCII 0.
 
Back
Top