流 reader.Read 个字符
Stream reader.Read number of character
是否有任何 Stream reader Class 只读取 string
中的 char
或 byte[]
中的 byte
?
例如读取字符串:
string chunk = streamReader.ReadChars(5); // Read next 5 chars
或读取字节
byte[] bytes = streamReader.ReadBytes(5); // Read next 5 bytes
请注意,此方法的 return 类型或 class 的名称并不重要。我只是想知道有没有类似的东西然后我可以用它。
我有 byte[]
来自 midi 文件。我想用 C# 读取这个 midi 文件。但我需要能够读取字节数。或字符(如果我将其转换为十六进制)。验证 midi 并更轻松地从中读取数据。
您可以通过执行以下操作来实现此目的,但我不确定这是否适用于您的问题。
StreamReader sr = new StreamReader(stream);
StringBuilder S = new StringBuilder();
while(true)
{
S = S.Append(sr.ReadLine());
if (sr.EndOfStream == true)
{
break;
}
}
一旦你在 "S" 上有了价值,你就可以考虑从中提取子字符串。
感谢您的评论。我不知道读取方法有重载。我可以用 FileStream 实现这个。
using (FileStream fileStream = new FileStream(path, FileMode.Open))
{
byte[] chunk = new byte[4];
fileStream.Read(chunk, 0, 4);
string hexLetters = BitConverter.ToString(chunk); // 4 Hex Letters that i need!
}
是否有任何 Stream reader Class 只读取 string
中的 char
或 byte[]
中的 byte
?
例如读取字符串:
string chunk = streamReader.ReadChars(5); // Read next 5 chars
或读取字节
byte[] bytes = streamReader.ReadBytes(5); // Read next 5 bytes
请注意,此方法的 return 类型或 class 的名称并不重要。我只是想知道有没有类似的东西然后我可以用它。
我有 byte[]
来自 midi 文件。我想用 C# 读取这个 midi 文件。但我需要能够读取字节数。或字符(如果我将其转换为十六进制)。验证 midi 并更轻松地从中读取数据。
您可以通过执行以下操作来实现此目的,但我不确定这是否适用于您的问题。
StreamReader sr = new StreamReader(stream);
StringBuilder S = new StringBuilder();
while(true)
{
S = S.Append(sr.ReadLine());
if (sr.EndOfStream == true)
{
break;
}
}
一旦你在 "S" 上有了价值,你就可以考虑从中提取子字符串。
感谢您的评论。我不知道读取方法有重载。我可以用 FileStream 实现这个。
using (FileStream fileStream = new FileStream(path, FileMode.Open))
{
byte[] chunk = new byte[4];
fileStream.Read(chunk, 0, 4);
string hexLetters = BitConverter.ToString(chunk); // 4 Hex Letters that i need!
}