简体   繁体   中英

Using LSet from VB6 to C#?

I'm currently converting VB6 to C#. Can you help me convert this code?

Here's the VB6 code:

'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

and this

Private Type bByteType
    bBytes(3) As Byte
End Type

Private Type SingleType
    dResult As Single
End Type

I tried my best to convert this into C#. But I'm getting a NullException. I just can't convert the Type from Vb6 into C#. So, I tried struct . But I have no idea how to transfer the bBytes data into tmpdata using C#.

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;
    }

The VB6 code is swapping over the byte order to convert between big-endian and little-endian. LSet in VB6 copies the byte values from one structure (Type ) to another even when the structure definitions are completely different. The binary data from one variable is copied into the memory space of the other, without regard for the data types specified for the elements. Gulp!

The best way to do this in C# is something like this answer .

It would be much more complicated in C# to copy the byte values from one structure to another - for instance you would need to pin the structures in memory to stop them moving around partway through the operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM