我如何更改此代码以便它写入字符串?
你好,我有这部分代码:
static void Main(string[] args)
{
Console.WriteLine("Memory mapped file reader started");
using (var file = MemoryMappedFile.OpenExisting("sensor"))
{
using (var reader = file.CreateViewAccessor(0, 3800))
{
var bytes = new byte[4051];
Console.WriteLine("Reading bytes");
for (var i = 0; i < bytes.Length; i++)
Console.Write((char)bytes[i] + "");
Console.WriteLine(string.Empty);
}
}
Console.WriteLine("Press any key to exit ...");
Console.ReadLine();
}
它打开共享内存,然后将其写入 var bytes 并显示它。我该如何将其写入字符串?我知道它与“var bytes = new byte[4051];”有关但显然我无法将“byte”写入新字符串。
PS 现在代码的输出(带有数组)是简单的文本:ABCDEFG...等
谢谢
hello i have this part of my code:
static void Main(string[] args)
{
Console.WriteLine("Memory mapped file reader started");
using (var file = MemoryMappedFile.OpenExisting("sensor"))
{
using (var reader = file.CreateViewAccessor(0, 3800))
{
var bytes = new byte[4051];
Console.WriteLine("Reading bytes");
for (var i = 0; i < bytes.Length; i++)
Console.Write((char)bytes[i] + "");
Console.WriteLine(string.Empty);
}
}
Console.WriteLine("Press any key to exit ...");
Console.ReadLine();
}
which opens the shared memory and then writes it to var bytes and displays it. how would i instead write it to a string? i know it has something to do with "var bytes = new byte[4051];" but i cant write "byte" to a new string obviously.
PS the output of the code now(with the array) is simple text: ABCDEFG... etc
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要写入的是文本信息,只需选择编码,然后对数据使用
GetString
即可。如果您想以文本方式显示二进制数据(如十六进制),那么您将需要扩展方法或以其他方式对其进行转换。
If it's textual information you're trying to write, just pick encoding, and use
GetString
on the data.If it's binary data you want to display textually (as in hex), then you'll need an extension method or otherwise to convert it.