C#中的定长字符串或结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/415214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:32:03  来源:igfitidea点击:

Fixed length strings or structures in C#

c#.netvb.net

提问by tonyriddle

I need to create a structure or series of strings that are fixed lenght for a project I am working on. Currently it is written in COBOL and is a communication application. It sends a fixed length record via the web and recieves a fixed length record back. I would like to write it as a structure for simplicity, but so far the best thing I have found is a method that uses string.padright to put the string terminator in the correct place.

我需要为我正在处理的项目创建一个固定长度的结构或一系列字符串。目前它是用 COBOL 编写的,是一个通信应用程序。它通过网络发送一个固定长度的记录并接收一个固定长度的记录。为了简单起见,我想把它写成一个结构,但到目前为止,我发现的最好的方法是使用 string.padright 将字符串终止符放在正确的位置。

I could write a class that encapsulates this and returns a fixed length string, but I'm hoping to find a simple way to fill a structure and use it as a fixed length record.

我可以编写一个类来封装它并返回一个固定长度的字符串,但我希望找到一种简单的方法来填充结构并将其用作固定长度的记录。

edit--

编辑 -

The fixed length record is used as a parameter in a URL, so its http:\somewebsite.com\parseme?record="firstname lastname address city state zip". I'm pretty sure I won't have to worry about ascii to unicode conversions since it's in a url. It's a little larger than that and more information is passed than address, about 30 or 35 fields.

定长记录在URL中作为参数使用,所以它的http:\somewebsite.com\parseme?record="firstname lastname address city state zip"。我很确定我不必担心 ascii 到 unicode 的转换,因为它位于 url 中。它比那个大一点,传递的信息比地址多,大约有 30 或 35 个字段。

采纳答案by Paul Whitehurst

As an answer, you should be able to use char arrays of the correct size, without having to marshal.

作为答案,您应该能够使用正确大小的字符数组,而无需编组。

Also, the difference between a class and a struct in .net is minimal. A struct cannot be null while a class can. Otherwise their use and capabilities are pretty much identical.

此外,.net 中的类和结构之间的差异很小。结构不能为空,而类可以。否则,它们的用途和功能几乎相同。

Finally, it sounds like you should be mindful of the size of the characters that are being sent. I'm assuming (I know, I know) that COBOL uses 8bit ASCII characters, while .net strings are going to use a UTF-16 encoded character set. This means that a 10 character string in COBOL is 10 bytes, but in .net, the same string is 20 bytes.

最后,听起来您应该注意发送的字符的大小。我假设(我知道,我知道)COBOL 使用 8 位 ASCII 字符,而 .net 字符串将使用 UTF-16 编码的字符集。这意味着 COBOL 中的 10 个字符的字符串是 10 个字节,但在 .net 中,相同的字符串是 20 个字节。

回答by Jonathan Allen

Add the MarshalAs tag to your structure. Here is an example:

将 MarshalAs 标记添加到您的结构中。下面是一个例子:

<StructLayout (LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure OSVERSIONINFO
    Public dwOSVersionInfoSize As Integer
    Public dwMajorVersion As Integer
    Public dwMinorVersion As Integer
    Public dwBuildNumber As Integer
    Public dwPlatformId As Integer

    <MarshalAs (UnmanagedType.ByValTStr, SizeConst:=128)> _
    Public szCSDVersion As String
End Structure

http://bytes.com/groups/net-vb/369711-defined-fixed-length-string-structure

http://bytes.com/groups/net-vb/369711-defined-fixed-length-string-structure

回答by Mark Brackett

You could use the VB6 compat FixedLengthStringclass, but it's pretty trivial to write your own.

您可以使用 VB6 兼容FixedLengthString类,但编写自己的类非常简单。

If you need parsing and validation and all that fun stuff, you may want to take a look at FileHelperswhich uses attributes to annotate and parse fixed length records.

如果您需要解析和验证以及所有有趣的东西,您可能需要查看FileHelpers,它使用属性来注释和解析固定长度的记录

FWIW, on anything relatively trivial (data processing, for instance) I'd probably just use a ToFixedLength() extension method that took care of padding or truncating as needed when writing out the records. For anything more complicated (like validation or parsing), I'd turn to FileHelpers.

FWIW,在任何相对微不足道的事情上(例如数据处理),我可能只使用 ToFixedLength() 扩展方法,该方法在写出记录时根据需要处理填充或截断。对于任何更复杂的事情(如验证或解析),我会求助于 FileHelpers。