我想知道我是否可以知道 C# 中
string
的字节长度,有人知道吗?
您可以使用 ASCII 等编码通过使用
System.Text.Encoding
类来获取每个字节的字符。
或者试试这个
System.Text.ASCIIEncoding.Unicode.GetByteCount(string);
System.Text.ASCIIEncoding.ASCII.GetByteCount(string);
来自MSDN:
对象是表示字符串的String
对象的顺序集合。System.Char
所以你可以使用这个:
var howManyBytes = yourString.Length * sizeof(Char);
System.Text.ASCIIEncoding.Unicode.GetByteCount(yourString);
或者
System.Text.ASCIIEncoding.ASCII.GetByteCount(yourString);
string
将占用多少字节取决于您选择的编码(或者在您不知情的情况下在后台自动选择)。此示例代码显示了差异:
using System;
using System.Text;
static void Main()
{
Encoding testedEncodings = new[]
{
Encoding.ASCII, // Note that '🡪' cannot be encoded in ASCII, data loss will occur
Encoding.UTF8, // This should always be your choice nowadays
Encoding.Unicode, // This is UTF-16. It is used by .NET to store your strings in RAM when the application is running, but this isn't useful information unless you're trying to manipulate bytes in RAM
Encoding.UTF32
};
string text = "a🡪";
Console.WriteLine($"Tested string: {text}");
Console.WriteLine($"String length: {text.Length}");
Console.WriteLine();
PrintTableHeader("Encoding", "Bytes", "Decoded string");
foreach (var encoding in testedEncodings)
{
byte[] bytes = encoding.GetBytes(text);
string decodedString = encoding.GetString(bytes);
PrintTableRow(
encoding.EncodingName,
$"{bytes.Length} ({string.Join(' ', bytes)})",
decodedString);
}
}
static void PrintTableHeader(params string[] values)
{
PrintTableRow(values);
Console.WriteLine(new string('-', 60));
}
static void PrintTableRow(params string[] values)
{
Console.WriteLine("{0,-16} | {1,-24} | {2}", values);
}
输出:
Tested string: a🡪
String length: 3
Encoding | Bytes | Decoded string
------------------------------------------------------------
US-ASCII | 3 (97 63 63) | a??
Unicode (UTF-8) | 5 (97 240 159 161 170) | a🡪
Unicode | 6 (97 0 62 216 106 220) | a🡪
Unicode (UTF-32) | 8 (97 0 0 0 106 248 1 0) | a🡪