如何在 C# 中将固定字节/char[100] 转换为托管 char[]?
在 C# 中将固定字节或 char[100] 转换为托管 char[] 的最佳方法是什么? 我最终不得不使用指针算术,我想知道是否有更简单的方法——比如 memcpy 或其他方法?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace StructTest
{
[StructLayout(LayoutKind.Explicit)]
unsafe struct OuterType
{
private const int BUFFER_SIZE = 100;
[FieldOffset(0)]
private int transactionType;
[FieldOffset(0)]
private fixed byte writeBuffer[BUFFER_SIZE];
public int TransactionType
{
get { return transactionType; }
set { transactionType = value; }
}
public char[] WriteBuffer
{
set
{
char[] newBuffer = value;
fixed (byte* b = writeBuffer)
{
byte* bptr = b;
for (int i = 0; i < newBuffer.Length; i++)
{
*bptr++ = (byte) newBuffer[i];
}
}
}
get
{
char[] newBuffer = new char[BUFFER_SIZE];
fixed (byte* b = writeBuffer)
{
byte* bptr = b;
for (int i = 0; i < newBuffer.Length; i++)
{
newBuffer[i] = (char) *bptr++;
}
}
return newBuffer;
}
}
}
class Program
{
static void Main(string[] args)
{
OuterType t = new OuterType();
t.WriteBuffer = "hello there".ToCharArray();
System.Console.WriteLine(t.WriteBuffer);
}
}
}
What's the best way to convert a fixed byte or char[100] to a managed char[] in C#? I ended up having to use pointer arithmetic and I'm wondering if there is an easier way -- something like a memcpy or another way?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace StructTest
{
[StructLayout(LayoutKind.Explicit)]
unsafe struct OuterType
{
private const int BUFFER_SIZE = 100;
[FieldOffset(0)]
private int transactionType;
[FieldOffset(0)]
private fixed byte writeBuffer[BUFFER_SIZE];
public int TransactionType
{
get { return transactionType; }
set { transactionType = value; }
}
public char[] WriteBuffer
{
set
{
char[] newBuffer = value;
fixed (byte* b = writeBuffer)
{
byte* bptr = b;
for (int i = 0; i < newBuffer.Length; i++)
{
*bptr++ = (byte) newBuffer[i];
}
}
}
get
{
char[] newBuffer = new char[BUFFER_SIZE];
fixed (byte* b = writeBuffer)
{
byte* bptr = b;
for (int i = 0; i < newBuffer.Length; i++)
{
newBuffer[i] = (char) *bptr++;
}
}
return newBuffer;
}
}
}
class Program
{
static void Main(string[] args)
{
OuterType t = new OuterType();
t.WriteBuffer = "hello there".ToCharArray();
System.Console.WriteLine(t.WriteBuffer);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Marshal.Copy 来实现此目的。 请注意,它还重载了 byte[],这可能是更合适的数据类型。
You can use Marshal.Copy for that. Notice it is also overloaded for byte[], which might be a more appropriate data type.
我不知道对固定变量进行转换的更好方法。 然而,使这一过程变得更简单的一种方法是完全避免使用固定变量。 相反,使用普通的 C# 数组并将其标记为 UnmanagedType.ByValArray
然后您可以使用简单的 LINQ 查询来转换数据。 完整解决方案如下
I don't know of a better way to do the conversion on a fixed variable. However one way to make this simpler is to avoid the use of a fixed variable altogether. Instead use a normal C# array and mark it as a UnmanagedType.ByValArray
Then you can use a simple LINQ query to translate the data. Full solution below