代码之家  ›  专栏  ›  技术社区  ›  tonyriddle

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

  •  2
  • tonyriddle  · 技术社区  · 17 年前

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

    编辑--

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

    3 回复  |  直到 17 年前
        1
  •  4
  •   Jonathan Allen    17 年前

    <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

        2
  •  1
  •   Mark Brackett    17 年前

    您可以使用VB6兼容程序 FixedLengthString 类,但编写自己的。

    FileHelpers 它使用属性 annotate and parse fixed length records .

        3
  •  0
  •   Fernando Vera    5 年前
    byte[] arrayByte = new byte[35];
    
    // Fill arrayByte with the other function
    
    //Convert array of byte to string chain
    string arrayStringConverr = Encoding.ASCII.GetString(arrayByte, 0, arrayByte.Length);
    
        4
  •  -2
  •   Paul Whitehurst    17 年前

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

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

    推荐文章