【发布时间】:2012-08-03 07:35:54
【问题描述】:
我们执行基于协议的数据发送到设备,设备需要格式化的数据包。
样本数据包数据为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
【问题讨论】:
-
这不是 VB6 语句。你是从什么转换而来的,最终结果应该是什么?
-
能否出示原码?
-
@MarkBertenshaw :放置示例代码和预期结果...
-
查看
String.Pad*方法,可能与.Length和.SubString()结合使用。 -
@Deanna yes 会尝试实施。看起来它符合我的要求
标签: vb.net string vb6 vb6-migration