简体   繁体   English

c++代码转换为vb.net计算错误

[英]c++ code convert to vb.net calculation error

the c++ code: c++ 代码:

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我将其转换为 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?在 c++ 结果为 5,vb.net 为 8。如何正确转换?

Got it:知道了:

    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()
    If (buffer.Length - data.Length + 3) Mod 4 = 0 Then
        Dim len = (buffer.Length - data.Length + 3) / 4 * 4
        data = buffer.Skip(len).ToArray
    Else
        Dim len = (buffer.Length - data.Length + 3) / 4 * 4 - 1
        data = buffer.Skip(len).ToArray
    End If
    Dim result As Integer = data(0)

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.我不确定您要实现什么,但是在 c++ 代码中,您将变量缓冲区初始化为 CHAR 数组,这意味着您将 1 转换为 char 1、2 转换为 char 2 等...如果您使用断点进行调试,您会看到 1 将转换为 '\x1',例如 '\a' 中的 7...然后将数据初始化为指针,它指向缓冲区的第 5 个元素。 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.然后你将 data 的值作为 int 检索,它会是 5。

In vb.net you can't convert from char to integer and vice versa.在 vb.net 中,您不能从 char 转换为 integer ,反之亦然。 So you can't really convert your c++ code.所以你不能真正转换你的 c++ 代码。

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).此外,在 vb.net 代码中,将缓冲区初始化为 16 个元素的数组,然后将数据初始化为 12 个元素的数组,从 5 开始(这次不是指针)。 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.然后初始化len,这将是7,因为你有16 - 12 + 3 = 7,然后你将7除以4,得到7。然后你告诉data再次是一个字节数组,但这次不是跳过缓冲区的前 4 个元素,而是跳过前 7 个(len)元素……当然,结果是 8。

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因为对我来说不是很清楚,并且某些操作在 vb.net 中是不可能的

This is the original code这是原始代码

void GetDialogEx(char *buf, CStrID id)

{ struct DialogBoxHeaderEx { DWORD SignEx; { 结构 DialogBoxHeaderEx { DWORD SignEx; DWORD Version;双字版本; DWORD ExStyle;双字样式; DWORD Style;双字样式; WORD DlgItems; WORD DlgItems; WORD x;字×; WORD y;字y; WORD cx;词 cx; WORD cy;单词 cy; WCHAR menuName[1]; WCHAR 菜单名[1]; } Header = (DialogBoxHeaderEx )buf; } 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 #结束区域

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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