Write string to fixed-length byte array in C#

Posted by toasteroven on Stack Overflow See other posts from Stack Overflow or by toasteroven
Published on 2010-04-07T20:19:17Z Indexed on 2010/04/07 20:23 UTC
Read the original article Hit count: 184

Filed under:
|
|

somehow couldn't find this with a google search, but I feel like it has to be simple...I need to convert a string to a fixed-length byte array, e.g. write "asdf" to a byte[20] array. the data is being sent over the network to a c++ app that expects a fixed-length field, and it works fine if I use a BinaryWriter and write the characters one by one, and pad it by writing '\0' an appropriate number of times.

is there a more appropriate way to do this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about string