我们执行基于协议的数据发送到设备需要格式化数据包的设备。
样本数据包数据是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