简体   繁体   English

如何存储长度为前缀的字符串

[英]How to store a length prefixed string

I have a program that receives network packets that are binary serialized. 我有一个程序可以接收二进制序列化的网络数据包。 I currently have a way of storing the data directly from the byte array into the struct by way of using pointers. 我目前有一种通过使用指针将数据直接从字节数组存储到结构的方法。 But what I need now is a way to store UTF strings in the struct as well whilst still maintaining the easy method of copying from the byte array. 但是我现在需要的是一种在结构中存储UTF字符串的方法,同时仍然保持从字节数组复制的简单方法。 The string is assumed to be prefixed with an unsigned short indicating the length in bytes of the string. 假定该字符串的前缀为无符号短,表示该字符串的字节长度。 Is this how C# internally stores char[] or strings or do I need to make my own struct to store it in? 这是C#内部存储char []或字符串的方式,还是我需要制作自己的结构以将其存储在其中?

My current struct layout is like this: 我当前的结构布局是这样的:

public struct GenericPacket
{
    public short packetid;

    public static GenericPacket ReadUsingPointer(byte[] data)
    {
        unsafe
        {
            fixed (byte* packet = &data[0])
            {
                return *(GenericPacket*)packet;
            }
        }
    }
}

Read using pointer simply takes a byte array and returns a pointer to it but as a GenericStruct. 使用指针读取只需要一个字节数组并以GenericStruct形式返回指向它的指针。 This works for every normal types, but strings are more complex. 这适用于每种普通类型,但字符串更复杂。

Thanks for any ideas! 感谢您的任何想法!

That is a BSTR and BSTR methods would work on it (available in Win32 and Win32 compatability libraries) - if your prefix refers to the bytes, not the character count, since characters can be multiple bytes; 这就是BSTR,BSTR方法就可以使用它(在Win32和Win32兼容性库中可用)-如果前缀是字节,而不是字符数,因为字符可以是多个字节。 BSTR doesn't know about any of that. BSTR对此一无所知。

C-string is null-terminated with no prefix, C++ and C# strings use implementation-defined storage, although they generally use an array with a separate length parameter. C字符串以空值结尾,没有前缀,C ++和C#字符串使用实现定义的存储,尽管它们通常使用带有单独的length参数的数组。 They're definitely not BSTRs. 它们绝对不是BSTR。

您可以使用System.Text.Encoding.GetString(byte [])或System.Text.Encoding.UTF8.GetString(byte [])来返回字符串

You can construct a string from char[] like this 您可以像这样从char []构造一个字符串

char[] foo = new char[] {'a','b','c','d'}
string bar = new string(foo);

Combine this with a ranged array copy to extract a string from a larger block of bytes. 将其与远程数组副本结合以从更大的字节块中提取字符串。

I believe this is what System.Text.Encoding.GetString(byte[] data, int start, int length) actually does internally, but in the .NET Micro Framework this is not available so you have to spell it out. 我相信这是System.Text.Encoding.GetString(byte []数据,int开头,int长度)实际在内部执行的操作,但是在.NET Micro Framework中,此功能不可用,因此您必须将其拼写出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM