在C#中固定长度的字符串或结构 [英] Fixed length strings or structures in C#

查看:675
本文介绍了在C#中固定长度的字符串或结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个固定lenght的一个项目我工作的弦的结构或系列。目前,它是用COBOL编写的,是一个通信应用。它发出了一个固定长度的记录通过网络和临危一个固定长度的记录了。我想将它写为简单的结构,但到目前为止,我已经找到了最好的办法是使用string.padright把一个字符串结束在正确的位置的方法。

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.

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

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.

编辑 -

固定长度的记录被用作一个URL参数,所以它的http:\ somewebsite.com \ parseme纪录=名字姓氏地址城市州邮政编码。我是pretty的肯定,我不会担心ASCII到UNI code的转换,因为它是在一个网址。这比那个大一点,信息传递不是地址,约30或35场。

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.

推荐答案

作为一个答案,你应该能够使用正确尺寸的字符数组,而不必元帅。

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

此外,在.NET中类和结构之间的差异很小。一个结构不能为空,而一个类可以。否则,它们的用途和功能是pretty的很多是相同的。

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.

最后,这听起来像你应该当心,正在发送字符的大小。我假设(我知道,我知道)COBOL使用8位ASCII字符,而.NET字符串要使用UTF-16连接codeD字符集。这意味着,在COBOL 10个字符的字符串是10个字节,但在.NET中,相同的字符串是20个字节。

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.

这篇关于在C#中固定长度的字符串或结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆