Home > Net >  c code convert to vb.net calculation error
c code convert to vb.net calculation error

Time:12-15

the c code:

char buffer[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
char* data = buffer   4;
data = (data - buffer   3) / 4 * 4   buffer;
int result = data[0];

I convert it to vb.net

    Dim buffer() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
    Dim data() As Byte = buffer.Skip(4).ToArray()
    Dim len = (buffer.Length - data.Length   3) \ 4 * 4
    data = buffer.Skip(len).ToArray
    Dim result As Integer = data(0)

Calculation results are inconsistent. in c result is 5, vb.net is 8.How can I convert it correctly?

CodePudding user response:

I'm not really sure what you are trying to achieve but, on the c code you are initializing a variable buffer as an array of CHAR, that means you'll convert 1 into char 1, 2 into char 2 etc... if you debug with a breakpoint you'll see that 1 will be converted into '\x1', 7 in '\a' for example... then you inizialize data as a pointer, and it points to the 5th element of buffer. Then you do something not really clear to me but it doesn't change the value of data. Then you retrieve the value of data as an int, and it's gonna be 5.

In vb.net you can't convert from char to integer and vice versa. So you can't really convert your c code.

Also, in the vb.net code, you inizialize buffer as an array of 16 elements, then you initialize data as an array of 12 elements, that starts from 5 (not a pointer this time). Then you inizialize len, which is gonna be 7, cause you have 16 - 12 3 = 7, and then you divide and multiply 7 by 4, which is gonna give you 7. Then you tell data to be again an array of bytes, but this time instead of skipping the first 4 elements of buffer, skip the first 7 (len) elements... Of course you'll get 8 as result.

If you can, please explain what you are trying to do here:

char* data = buffer   4;
data = (data - buffer   3) / 4 * 4   buffer;

Because it's not very clear to me and certain operations are not possible in vb.net

CodePudding user response:

This is the original code

void GetDialogEx(char *buf, CStrID id)

{ struct DialogBoxHeaderEx { DWORD SignEx; DWORD Version; DWORD ExStyle; DWORD Style; WORD DlgItems; WORD x; WORD y; WORD cx; WORD cy; WCHAR menuName[1]; } Header = (DialogBoxHeaderEx)buf;

if(Header->ExStyle) Print("EXSTYLE 0x%.8X\n", Header->ExStyle);
WCHAR *ClassName,*szCaption;
char *ItemData;

if(Header->menuName[0] == 0xFFFF)
{
    Print("MENU %d\n",Header->menuName[1]);
    ClassName = Header->menuName   2;
}
else if(Header->menuName[0])
{
    Puts("MENU \"");
    ClassName = (WCHAR*)WPuts(Header->menuName);
    Puts("\"\n");
}
else ClassName = Header->menuName   1;

if(*ClassName == 0xFFFF)
{
    Print("CLASS %d\n",ClassName[1]);
    szCaption = ClassName   2;
}
else if(*ClassName)
{
    Puts("CLASS \"");
    szCaption = (WCHAR*)WPuts(ClassName);
    Puts("\"\n");
}
else szCaption = ClassName   1;

if(*szCaption)
{
    Puts("CAPTION \"");
    ItemData = (char*)WPuts(szCaption);
    Puts("\"\n");
}
else ItemData = (char*)(szCaption   1);
if(Header->Style & DS_SETFONT)
{
    struct DialogFontEx
    {
        WORD wPointSize;
        WORD Weight;
        BYTE Italic;
        BYTE CharSet;
        WCHAR FontName[1];
    } *Font = (DialogFontEx*)ItemData;
    Print("FONT %d, \"", Font->wPointSize);
    ItemData = (char*)WPuts(Font->FontName);
    Print("\", %d, %d, %d\n", Font->Weight, Font->Italic, Font->CharSet);
}

Puts("BEGIN\n");
for(int i=0; i<Header->DlgItems; i  )
{
    ItemData = (ItemData - buf   3) /4*4   buf;
    ItemData = GetDialogItem(ItemData, true);
}
Puts("END\n");

}

and my converted code:

#Region "ControlList"
        Dim ctlData As Object
        If isDialogEx Then
            ctlData = New ControlDataEx()
        Else
            ctlData = New ControlData()
        End If
        Dim bItemData() As Byte = bFontData.Skip(2).ToArray()
        'bItemData = (bItemData.Length - bytesIn.Length   3) / 4 * 4   bItemData
        For i As Integer = 0 To DlgBoxHeader.DlgItems - 1
            len = (bytesIn.Length - bItemData.Length   3) \ 4
            bItemData = bytesIn.Skip(len * 4).ToArray

            size = Marshal.SizeOf(ctlData)
            buff = Marshal.AllocHGlobal(size)
            Marshal.Copy(bItemData.Take(size).ToArray(), 0, buff, size)
            If isDialogEx Then
                ctlData = CType(Marshal.PtrToStructure(buff, GetType(ControlDataEx)), ControlDataEx)
            Else
                ctlData = CType(Marshal.PtrToStructure(buff, GetType(ControlData)), ControlData)
            End If
            Marshal.FreeHGlobal(buff)
            'Debug.Print("Constol ID:"   ctlData.id.ToString)
            'Dim Style = If(isDialogEx, ctlData.Style, ctlData.ExStyle)
            'Dim styleValue = Style And &HF
            'Debug.Print("style:0x"   CInt(ctlData.style).ToString("x8")   " exstyle:0x"   CInt(ctlData.exstyle).ToString("x8")   " style value:"   styleValue.ToString)
            Dim bClassID() As Byte = bItemData.Skip(size   2).ToArray()
            If bClassID(0) = &HFF And bClassID(1) = &HFF Then '控件类型
                bClassID = bClassID.Skip(2).ToArray()
            End If
            If bClassID(0) = &H80 And bClassID(1) = 0 Then
                Debug.Print("Button")
            ElseIf bClassID(0) = &H81 And bClassID(1) = 0 Then
                Debug.Print("EditText")
            ElseIf bClassID(0) = &H82 And bClassID(1) = 0 Then
                Debug.Print("Static")
            ElseIf bClassID(0) = &H83 And bClassID(1) = 0 Then
                Debug.Print("ListBox")
            ElseIf bClassID(0) = &H84 And bClassID(1) = 0 Then
                Debug.Print("ScrollBar")
            ElseIf bClassID(0) = &H85 And bClassID(1) = 0 Then
                Debug.Print("ComboBox")
            ElseIf bClassID(0) <> 0 Then 
                len = GetUnicodeString(bClassID, outString)
                Debug.Print(outString)
                bClassID = bClassID.Skip(len).ToArray()
            End If
            bClassID = bClassID.Skip(2).ToArray()
            If bClassID(0) = &HFF And bClassID(1) = &HFF Then
                bClassID = bClassID.Skip(2).ToArray()
                len = GetUnicodeString(bClassID, outString)
                result.Add(text   ctlData.id.ToString()   ":"   outString)
                Debug.Print(text   ctlData.id.ToString()   ":"   outString)
                bClassID = bClassID.Skip(len).ToArray()
            Else
                len = GetUnicodeString(bClassID, outString)
                result.Add(text   ctlData.id.ToString()   ":"   outString)
                Debug.Print(text   ctlData.id.ToString()   ":"   outString)
                bClassID = bClassID.Skip(len).ToArray()
            End If
            bClassID = bClassID.Skip(2).ToArray
            Debug.Print(vbNewLine)
            If isDialogEx Then bItemData = bClassID.Skip(2).ToArray
        Next i
    Catch ex As Exception

    End Try

#End Region

  • Related