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
If it's textual information you're trying to write, just pick encoding, and use GetString
on the data.
var encoding = Encoding.ASCII;
Console.WriteLine(encoding.GetString(bytes));
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.
static string ToHex(this byte[] data) {
var builder = new StringBuilder(data.Length * 3);
foreach (var b in data)
buidler.Append(b.ToString("X2") + " ");
return builder.ToString();
}
....
Console.WriteLine(bytes.ToHex());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.