本文介绍了sprintf的在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有类似在C#中的东西的sprintf()
?
我想比如说想转换一个整型到2字节的字节数组
是这样的:
INT数= 17;
字节[] S = sprintf的(%2C号);
解决方案
原来,这是我真正想要的是这个
短号码= 17;
System.IO.BinaryWriter作家=新System.IO.BinaryWriter(流);
writer.Write(数);
writer.Flush();
这里的关键是类的BinaryWriter的写入功能。它有18个重载,转换成不同的格式为一个字节数组,它写入流。在我来说,我必须确保我想写保持在很短的数据类型的数量,这将使Write函数写入2个字节。
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:
int number = 17;
byte[] s = sprintf("%2c", number);
解决方案
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.
这篇关于sprintf的在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!