簡體   English   中英

使用 LSet 從 VB6 到 C#?

[英]Using LSet from VB6 to C#?

我目前正在將 VB6 轉換為 C#。 你能幫我轉換這個代碼嗎?

這是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。 我只是無法將Type從 Vb6 轉換為 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;
    }

VB6 代碼正在交換字節順序以在大端和小端之間進行轉換。 即使結構定義完全不同,VB6 中的LSet將字節值從一種結構 (Type ) 復制到另一種結構。 一個變量的二進制數據被復制到另一個變量的內存空間中,而不考慮為元素指定的數據類型。 呸!

在 C# 中執行此操作的最佳方法類似於此答案

在 C# 中將字節值從一個結構復制到另一個結構會復雜得多 - 例如,您需要將結構固定在內存中以阻止它們在操作中途移動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM