sprintf in C#?

前端 未结 3 772
余生分开走
余生分开走 2020-12-16 12:08

Is there something similar to sprintf() in C#?

I would for instance like to convert an integer to a 2-byte byte-array.

Something like:



        
3条回答
  •  庸人自扰
    2020-12-16 12:45

    It turned out, that what I really wanted was this:

    short number = 17;
    System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
    writer.Write(number);
    writer.Flush();
    

    The key here is the Write-function of the BinaryWriter class. It has 18 overloads, converting different formats to a byte array which it writes to the stream. In my case I have to make sure the number I want to write is kept in a short datatype, this will make the Write function write 2 bytes.

提交回复
热议问题