从VB6到C#使用LSet?

问题描述:

我目前正在将VB6转换为C#。你能帮我转换这个代码吗?从VB6到C#使用LSet?

这里的VB6代码:

'This converts the bytes into test parameters 
Sub getParamValues(ByRef TransData() As Byte, ByRef testparam() As Single, startbyte As Integer) 
Dim tmpdata As bByteType 
Dim convertedvalue As SingleType 
Dim i As Integer 
Dim bytecounter As Integer 
bytecounter = startbyte 

'On Error Resume Next 
For i = 0 To 9 
    tmpdata.bBytes(0) = TransData(bytecounter + 3) 'TransData(0 + 3) 
    tmpdata.bBytes(1) = TransData(bytecounter + 2) 'TransData(0 + 2) 
    tmpdata.bBytes(2) = TransData(bytecounter + 1) 'TransData(0 + 1) 
    tmpdata.bBytes(3) = TransData(bytecounter)  'TransData (0) 

    'THIS CODE I WANT TO CONVERT 
    LSet convertedvalue = tmpdata 

    testparam(i) = convertedvalue.dResult 'Gets the test parameters 
    bytecounter = bytecounter + 4 
Next i 
End Sub 

Private Type bByteType 
    bBytes(3) As Byte 
End Type 

Private Type SingleType 
    dResult As Single 
End Type 

我尽力将其转换为C#。但是我得到一个NullException。我无法将Vb6中的Type转换为C#。所以,我试过struct。但我不知道如何使用C#将bBytes数据转换为tmpdata

public void getParamValues(ref byte[] TransData, ref Single[] testparam, int startbyte) 
    { 
     bByteType tmpdata = new bByteType(); 
     SingleType convertedvalue = new SingleType(); 
     //byte[] bBytes = new byte[4]; 
     int bytecounter = 0; 
     bytecounter = startbyte; 

     for (int i = 0; i < 9; i++) 
     { 
      tmpdata.bBytes[0] = TransData[bytecounter + 3]; 
      tmpdata.bBytes[1] = TransData[bytecounter + 2]; 
      tmpdata.bBytes[2] = TransData[bytecounter + 1]; 
      tmpdata.bBytes[3] = TransData[bytecounter]; 
      //LSet convertedvalue = tmpdata <--- Supposed to convert to C#     
      testparam[i] = convertedvalue.dResult; 
      bytecounter = bytecounter + 4; 
     } 
    } 

public struct bByteType 
    { 
     //byte[] bBytes = new byte[3]; 
     public byte[] bBytes; 
     public bByteType(byte[] size) 
     { 
      bBytes = new byte[4]; 
     } 
    } 

    struct SingleType 
    { 
     public Single dResult; 
    } 
+0

可能的重复http://*.com/questions/3864422/converting-vb6-custom-type-with-fixed-length-strings-to-vb-net – Pikoh

+0

@Pikoh这个问题是关于不同的使用LSet从这个问题。尽管需要对VB6进行一些解码才能知道!这个问题是关于在big-endian和little-endian之间转换的,那个问题原来是关于操作固定长度的字符串的。 – MarkJ

+0

@MarkJ够公平的,你是对的 – Pikoh

VB6代码正在交换字节顺序以在大端和小端之间转换。在VB6中,LSet将字节值从一个结构(Type)复制到另一个结构,即使结构定义完全不同。来自一个变量的二进制数据被复制到另一个变量的存储空间中,而不考虑为这些元素指定的数据类型。 吞噬!

在C#中执行此操作的最佳方法是something like this answer

在C#中将字节值从一个结构复制到另一个结构会更复杂 - 例如,您需要将结构固定在内存中,以阻止它们在操作中途移动。