C#中的sprintf?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/313006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sprintf in C#?
提问by Markus Johansson
Is there something similar to sprintf()
in C#?
有没有类似于sprintf()
C# 的东西?
I would for instance like to convert an integer to a 2-byte byte-array.
例如,我想将整数转换为 2 字节字节数组。
Something like:
就像是:
int number = 17;
byte[] s = sprintf("%2c", number);
采纳答案by Markus Johansson
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.
这里的关键是 BinaryWriter 类的 Write-function。它有 18 个重载,将不同的格式转换成一个字节数组,然后写入流中。在我的情况下,我必须确保我要写入的数字保持在一个短数据类型中,这将使 Write 函数写入 2 个字节。
回答by Marc Gravell
string s = string.Format("{0:00}", number)
The first 0 means "the first argument" (i.e. number); the 00 after the colon is the format specifier (2 numeric digits).
第一个 0 表示“第一个参数”(即数字);冒号后的 00 是格式说明符(2 个数字)。
However, note that .NET strings are UTF-16, so a 2-character string is 4 bytes, not 2
但是,请注意 .NET 字符串是 UTF-16,因此 2 个字符的字符串是 4 个字节,而不是 2
(edit: question changed from string
to byte[]
)
(编辑:问题从string
改为byte[]
)
To get the bytes, use Encoding
:
要获取字节,请使用Encoding
:
byte[] raw = Encoding.UTF8.GetBytes(s);
(obviously different encodings may give different results; UTF8 will give 2 bytes for this data)
(显然不同的编码可能会产生不同的结果;UTF8 会为这个数据提供 2 个字节)
Actually, a shorter version of the first bit is:
实际上,第一位的较短版本是:
string s = number.ToString("00");
But the string.Format
version is more flexible.
但string.Format
版本更灵活。
回答by Yona
EDIT: I'm assuming that you want to convert the value of an integer to a byte array and not the value converted to a string first and then to a byte array (check marc's answer for the latter.)
编辑:我假设您想将整数的值转换为字节数组,而不是先将值转换为字符串,然后再转换为字节数组(检查 marc 对后者的回答。)
To convert an int to a byte array you can use:
要将 int 转换为字节数组,您可以使用:
byte[] array = BitConverter.GetBytes(17);
but that will give you an array of 4 bytes and not 2 (since an int is 32 bits.) To get an array of 2 bytes you should use:
但这会给你一个 4 个字节的数组而不是 2 个(因为 int 是 32 位。)要获得一个 2 个字节的数组,你应该使用:
byte[] array = BitConverter.GetBytes((short)17);
If you just want to convert the value 17 to two characters then use:
如果您只想将值 17 转换为两个字符,请使用:
string result = string.Format("{0:00}", 17);
But as marc pointed out the result will consume 4 bytes since each character in .NET is 2 bytes (UTF-16) (including the two bytes that hold the string length it will be 6 bytes).
但正如 marc 指出的,结果将消耗 4 个字节,因为 .NET 中的每个字符是 2 个字节(UTF-16)(包括保存字符串长度的两个字节,它将是 6 个字节)。