猿问

固定 sbyte[] 到字符串 C#

我正在尝试使用StrucLayout和从内存中获取字符串FieldOffset


但是我在理解byte工作原理时遇到了很多麻烦。


这是我的代码 ATM :


[StructLayout(LayoutKind.Explicit)]

public unsafe struct InfoDetails

{

    [FieldOffset(0x14)]

    public fixed sbyte Name[50];


    public string getName

    {

        get

        {

            fixed (sbyte* namePtr = Name)

            {

                return new string(namePtr);

            }

        }

    }

}

此代码returns:T。预期结果是TEZ。


关于我为什么做错的任何建议?谢谢


米琪卡哇伊
浏览 296回答 3
3回答

慕沐林林

您似乎对字符串编码有问题。考虑以下测试代码:unsafe{&nbsp; &nbsp; InfoDetails d;&nbsp; &nbsp; var encoding = Encoding.Unicode;&nbsp; &nbsp; var stringBytes = encoding.GetBytes("TEZ");&nbsp; &nbsp; for(int i=0; i<stringBytes.Length; i++) d.Name[i] = (sbyte)stringBytes[i];&nbsp; &nbsp; Console.WriteLine(d.getName);}您确实会得到“T”,但是如果您将编码更改Encoding.ASCII为预期的“TEZ”。解决方法:需要事先知道信息的编码,并据此生成字符串。看起来它是 Unicode,所以先试试这个:fixed (sbyte* namePtr = Name){&nbsp; return new string(namePtr, 0, 50, Encoding.Unicode);}

哈士奇WWW

他们帮助我找到了一个可行的解决方案。我真的不知道它是否是最好的:[StructLayout(LayoutKind.Explicit)]&nbsp; &nbsp; public unsafe struct InfoDetails&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [FieldOffset(0x14)]&nbsp; &nbsp; &nbsp; &nbsp; public fixed byte Name[50];&nbsp; &nbsp; &nbsp; &nbsp; public string test&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<byte> clearBytes = new List<byte>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fixed (byte* namePtr = Name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 50; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (namePtr[i] == 0x0 && namePtr[i + 1] == 0x0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clearBytes.Add(namePtr[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (clearBytes.Count() % 2 != 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clearBytes.Add(0x00);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Encoding.Unicode.GetString(clearBytes.ToArray());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答