我正试图将C代码转换为C#,却偶然发现了一行代码,但我在转换时遇到了问题。
sprintf((char*)&u8FirmareBuffer[0x1C0] + strlen((char*)&u8FirmareBuffer[0x1C0]), ".B%s", argv[3]);
特别是这一行。 u8FirmwareBuffer是C中的一个无符号char数组,我猜是C#中的一个字节数组。 argv [3]是一个字符串。 如何将这行代码转换为C#。
谢谢您的帮助。
编辑:这已被标记为重复项,但我认为它们之间存在差异,因为我使用的指针不适用于标记的帖子上提供的解决方案。
答案 0 :(得分:1)
您可以执行以下操作:
string myString = "This is my string";
byte[] buffer = new byte[1024];
int offset = 0;
// if you pass a byte buffer to the constructor of a memorystream, it will use that, don't forget that it cannot grow the buffer.
using (var memStream = new MemoryStream(buffer))
{
// you can even seek to a specific position
memStream.Seek(offset, SeekOrigin.Begin);
// check your encoding..
var data = Encoding.UTF8.GetBytes(myString);
// write it on the current offset in the memory stream
memStream.Write(data, 0, data.Length);
}
string myString = "This is my string";
byte[] buffer = new byte[1024];
int offset = 0;
// if you pass a byte buffer to the constructor.....(see above)
using (var memStream = new MemoryStream(buffer))
using (var streamWriter = new StreamWriter(memStream))
{
// you can even seek to a specific position
memStream.Seek(offset, SeekOrigin.Begin);
streamWriter.Write(myString);
streamWriter.Flush();
// don't forget to flush before you seek again
}