0

我们执行基于协议的数据发送到设备需要格式化数据包的设备。

样本数据包数据是XXFSXXXFSXXXXXXXFSXXXXXX. 提到的X是每个字符串的最大长度大小。如果数据小于字符串最大长度,它应该用 NULL 字符填充..11FS123FS1234XXXX(剩余的 X 将用 NULL 填充)。

我只是想将 VB6 函数之一转换为 VB.Net,下面是我面临问题的转换语句

Option Strict Off
Option Explicit On
Imports Microsoft.VisualBasic.Compatibility.VB6
Imports System
Imports System.Runtime.InteropServices
Module FunctionCmd_Msg

    Public FunCommand_Msg As Fun_CommandMessage = Fun_CommandMessage.CreateInstance()
    'Function Command Message
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _ _
    Public Structure Fun_CommandMessage
        <VBFixedString(1)> Public one As String
        <VBFixedString(1)> Public two As String
        <VBFixedString(3)> Public three As String
        Dim five As String
        <VBFixedString(8)> Public four As String
        Public Shared Function CreateInstance() As Fun_CommandMessage
            Dim result As New Fun_CommandMessage
            result.one = String.Empty
            result.two = String.Empty
            result.three = String.Empty
            result.four = String.Empty
            result.five = String.Empty
        End Function
    End Structure
End Module

假设:

one = "1"
two = "1"
three = "123"
four = "5678"           
five = "testing"
FS = character (field separator)

在连接字符串时,我需要一个固定长度的字符串,如下所示:

one & two & FS & three & FS & five & FS & four

输出:因为四是一个8长度的固定长度字符串,剩下的4个字符应该用null填充,如下所示

11 FS 123 FS testing FS 5678XXXX
4

2 回答 2

6

固定长度的字符串在 .NET 中不再有意义。Microsoft 试图提供一个类似的类以便于升级,但事实是您应该根据使用情况更改代码:

固定长度字符串在您的 VB6 代码中做了什么?是不是没有充分的理由?String然后在 .NET 中使用普通的。

是为了与 C API 互操作吗?然后使用编组在 C API 调用中设置数组的大小。

于 2012-08-06T10:30:17.380 回答
0

忘记固定长度,使用常规的 vb.net 字符串。他们会很好地返回任何调用该代码的代码,包括互操作。

所以,只要垫好你的弦,你就可以参加比赛了。

事实上,构建一个 Msg 类来为你做这些脏活。

这对我来说看起来很不错:

注意我是如何设置的,你只在一个地方定义字符串的长度。(所以我使用 len(m_string) 来确定代码中从 THEN 开始的长度。

此外,对于调试和此示例,我使用 X 代替 vbcharNull(您应该使用它)进行测试。

现在,在你的代码中?

只需使用这个:

    Dim Msg As New MyMsg

    With Msg
        .one = "A"
        .two = "B"
        .three = "C"
        .four = "D"
        .Five = "E"
    End With

    Debug.WriteLine(Msg.Msg("*") & vbCrLf)

    Debug.WriteLine("total lenght = " & Len(Msg.Msg("X").ToString))

输出:

A*B*CXX*EXXXXXXX*DXXXXXXX

total lenght = 25

我在您的代码中注意到您在四个之前有五个 - 但如果这是您想要的,那么没问题

请注意,该类始终为您维护长度。

因此,只需将此代码粘贴到您的模块甚至是新的单独类中。

Public Class MyMsg
    'Dim cPad As Char = vbNullChar
    Dim cPad As Char = "X"

    Private m_one As String = New String(cPad, 1)
    Private m_two As String = New String(cPad, 1)
    Private m_three As String = New String(cPad, 3)
    Private m_four As String = New String(cPad, 8)
    Private m_five As String = New String(cPad, 8)

    Public Property one As String
        Get
            Return m_one
        End Get
        Set(value As String)
            m_one = MyPad(value, m_one)
        End Set
    End Property
    Public Property two As String
        Get
            Return m_two
        End Get
        Set(value As String)
            m_two = MyPad(value, m_two)
        End Set
    End Property

    Public Property three As String
        Get
            Return m_three
        End Get
        Set(value As String)
            m_three = MyPad(value, m_three)
        End Set
    End Property

    Public Property four As String
        Get
            Return m_four
        End Get
        Set(value As String)
            m_four = MyPad(value, m_four)
        End Set
    End Property

    Public Property Five As String
        Get
            Return m_five
        End Get
        Set(value As String)
            m_five = MyPad(value, m_five)
        End Set
    End Property


    Public Function Msg(FS As String) As String
        Return m_one & FS & m_two & FS & m_three & FS & m_five & FS & m_four
    End Function

    Private Function MyPad(str As String, strV As String) As String
        Return Strings.Left(str & New String(Me.cPad, Len(strV)), Len(strV))
    End Function

End Class

如前所述,将 char 的注释掉的“X”行更改回 vbCharNull。

当然,您仍然可以选择分隔符。我用了

Msg.Msg("*") 

所以我使用了“*”,但你可以使用空格或任何你想要的东西。

于 2019-03-21T00:35:49.167 回答