Results 1 to 6 of 6

Thread: small help converting c++ to vb6

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2003
    Posts
    43

    small help converting c++ to vb6

    hi mates. i have this c++ code:

    Code:
    #include <string.h>
    
    static unsigned char table1[ ][ 16 ] = {
    	{ 0x0C, 0x2D, 0x22, 0x43, 0x17, 0x02, 0x13, 0x0A, 0x0F, 0x63, 0x4C, 0x2D, 0x5B, 0x03, 0x37, 0x3B },
    	{ 0x05, 0x17, 0x43, 0x4C, 0x0C, 0x16, 0x4E, 0x13, 0x59, 0x32, 0x2B, 0x38, 0x56, 0x0B, 0x0F, 0x63 }
    	};
    
    static unsigned int table2[ ][ 4 ] = {
    	{ 0x1E2D3E1F, 0xF5E6F798, 0x9E0D1DFF, 0x3E4D5A3E },
    	{ 0x7A8E9E0D, 0xB1E2D3A4, 0x5A6E7E8D, 0x9E0F2E3D }
    	};
    
    static unsigned int table3[] =	{
    	0x9E3779B9,
    	0x6F55A634
    	};
    
    void Vitel_calc( char *imei, char type, char *code )
    {
    	unsigned int x, y = 0;
    	unsigned int dest[ 8 ];
    	unsigned char IMEI[ 17 ];
    	int i, j;
    
    	strncpy( (char *)IMEI, imei, 14 );
    	IMEI[14] = '0';
    	IMEI[15] = '0';
    	IMEI[16] = 0;
    
    
    For I = 0 To 7
    DEST(I) = (TABLE1(Vitel_Type)(I Xor &HF) * (IMEI(&H10 - I) - &H30)) - ((IMEI(I) + IMEI(I Xor &HF)) Mod 9) + &H39
    next I
    
    
    	for( i = 0; i < 8; i++ )
    	{
    dest[ i ] = ( table1[ type ][ i ^ 0x0F ] * ( IMEI[ 0x10 - i ] - 0x30 )) - (( IMEI[ i ] + IMEI[ i ^ 0x0F ] ) % 9 ) + 0x39;
    	}
    
    	for( i = 0; i < 0x10; i++ )
    		y += IMEI[ i ];
    
    	for( i = 0; i < 8; i++ )
    	{
    		for( j = 0; j < 4; j++ )
    		{
    			x = table1[ type ][ ( i ^ 7 ) + j ];
    			dest[ i ^ 7 ] += (( x << 5 ) ^ ( x << 4 )) + table2[ type ][ j ^ 3 ];
    		}
    		dest[ i ^ 7 ] += ( y + table3[ type ] * ( i + 1 )) << 2;
    	}
    
    	for( i = 0; i < 8; i++ )
    	{
    		y = dest[ ( i + y ) % 6 ];
    		code[ i ] = (char)( 0x39 - ( y % 10 ));
    	}
    	code[ 8 ] = 0;
    }
    and i am trying to convert it to vb6

    i already got this:

    Code:
    Private Sub Command1_Click()
    Dim x, y, i, j, dest(8), Imei(21) As Integer
    Dim Vitel_Type
    Dim code(10) As Integer
    
    For i = 0 To 15
        Imei(i) = Val(Mid(Text2.Text, i + 1, 1)) + 48
    Next i
    
    y = 0
    Imei(14) = 0
    table1 = Array(Array(&HC, &H2D, &H22, &H43, &H17, &H2, &H13, &HA, &HF, &H63, &H4C, &H2D, &H5B, &H3, &H37, &H3B), Array(&H5, &H17, &H43, &H4C, &HC, &H16, &H4E, &H13, &H59, &H32, &H2B, &H38, &H56, &HB, &HF, &H63))
    table2 = Array(Array(&H1E2D3E1F, &HF5E6F798, &H9E0D1DFF, &H3E4D5A3E), Array(&H7A8E9E0D, &HB1E2D3A4, &H5A6E7E8D, &H9E0F2E3D))
    table3 = Array(&H9E3779B9, &H6F55A634)
    Vitel_Type = Text1.Text
    
    For i = 0 To 7
        dest(i) = (table1(Vitel_Type)(i Xor &HF) * (Imei(&H10 - i) - &H30)) - ((Imei(i) + Imei(i Xor &HF)) Mod 9) '+ &H39
    Next i
    
    For i = 0 To &H10
        y = y + Imei(i)
    Next i
    
    For i = 0 To 7
        For j = 0 To 3
            x = table1(Vitel_Type)((i Xor 7) + j)
            dest(i Xor 7) = dest(i Xor 7) + (CInt(x / (2 ^ 5)) Xor CInt(x / (2 ^ 4)) + table2(Vitel_Type)(j Xor 3))
        Next j
        dest(i Xor 7) = (dest(i Xor 7) + (y + table3(Vitel_Type) * (i + 1))) / (2 ^ 2)
    Next i
    
    For i = 0 To 7
        y = dest((i + y) Mod 6)
        code(i) = y Mod 10
    Next i
    code(8) = 0
    
    MsgBox code(0) + code(1) + code(2) + code(3) + code(4) + code(5) + code(6) + code(7) + code(8)
    End Sub
    but it is not working yet.

    this was supposed to calculate a code.

    for example:


    if i enter the number
    123456789012347


    it should calculate me this 2 codes depending if i enter 0 or 1 in text1.text:

    21477487
    91315163

    but i was not successfull to convert this.

    i am waiting for your help. thanks in advance mates.

  2. #2

    Thread Starter
    Member
    Join Date
    Dec 2003
    Posts
    43
    where did the edit button go? :s

    anyway, this is the most updated codew in vb6 i have

    not working yet

    Code:
    Private Sub Command1_Click()
    Dim x, y, i, j, dest(8), Imei(21) As Integer
    Dim Vitel_Type
    Dim code(10) As Integer
    
    For i = 0 To 15
        Imei(i) = Val(Mid(Text2.Text, i + 1, 1)) + 48
    Next i
    
    y = 0
    Imei(14) = 0
    table1 = Array(Array(&HC, &H2D, &H22, &H43, &H17, &H2, &H13, &HA, &HF, &H63, &H4C, &H2D, &H5B, &H3, &H37, &H3B), Array(&H5, &H17, &H43, &H4C, &HC, &H16, &H4E, &H13, &H59, &H32, &H2B, &H38, &H56, &HB, &HF, &H63))
    table2 = Array(Array(&H1E2D3E1F, &HF5E6F798, &H9E0D1DFF, &H3E4D5A3E), Array(&H7A8E9E0D, &HB1E2D3A4, &H5A6E7E8D, &H9E0F2E3D))
    table3 = Array(&H9E3779B9, &H6F55A634)
    Vitel_Type = Text1.Text
    
    For i = 0 To 7
        dest(i) = (table1(Vitel_Type)(i Xor &HF) * (Imei(&H10 - i) - &H30)) - ((Imei(i) + Imei(i Xor &HF)) Mod 9) '+ &H39
    Next i
    
    For i = 0 To 15
        y = y + Imei(i)
    Next i
    
    For i = 0 To 7
        For j = 0 To 3
            x = table1(Vitel_Type)((i Xor 7) + j)
            dest(i Xor 7) = dest(i Xor 7) + (CInt(x * (2 ^ 5)) Xor CInt(x * (2 ^ 4)) + table2(Vitel_Type)(j Xor 3))
        Next j
        dest(i Xor 7) = (dest(i Xor 7) + (y + table3(Vitel_Type) * (i + 1))) * (2 ^ 2)
    Next i
    
    For i = 0 To 7
        y = dest((i + y) Mod 6)
        code(i) = y Mod 10
    Next i
    code(8) = 0
    
    MsgBox code(0) + code(1) + code(2) + code(3) + code(4) + code(5) + code(6) + code(7) + code(8)
    End Sub
    This forum rocks!

  3. #3
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    The C++ arrays are unsigned integers. This datatype is a 32 bit unsigned integer, while the Long datatype is a signed 32 bit integer. Unfortunately there are no unsigned variants of Integer and Long in VB. (Not sure whether Byte is unsigned or not.)

    There are a couple of other errors as well. The << operator is the left shift operator. It shifts bits to the left. If you need to use it in VB, then you need to multiply a number with 2^(number to shift).
    (The >> operator, right shift, not present here, does the opposite. If you use it, make sure you're doing an integer division.)
    When trying to convert the left shift operator, make sure your resulting number doesn't accidentally turn into a double when it grows too large, since the binary information will be modified then. (It's more likely that an overflow error will occur)
    It's better to use precalculated values, instead of calculating 2^5 and 2^4 all the time. (Powers, not Xor.).
    Also, CInt, will cut off the calculated values to 16 bits, so the 16 most significant bits are discarded.

    My suggestion to you if you want to "translate" this code: make sure that you understand the algorithm. The code is a mere representation of the algorithm, so that it can be compiled into executable code. Your VB code might look quite much after the C++ code, but it's impossible to do an 1:1 translation.
    Last edited by riis; Aug 13th, 2004 at 04:03 PM.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    When I first read this, I thought a worthwhile move might be to shift this to .NET. I guess I might as well make that suggestion now. .NET allows for 64bit integers, so the unsigned ints can be turned into those.

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    If you want the code to get a VB alternative, you should give more example results. It is impossible to code an alternative with just one, at easiest you could just do:

    VB Code:
    1. 'in a module
    2. Public Function GetCode(ByVal CodeString As String, CodeMode As Boolean) As String
    3.     Dim Code(15) As Byte, IMEI() As Byte
    4.     Dim A As Long, B As Long
    5.    
    6.     IMEI = CodeString
    7.     If Not(UBound(IMEI) = 31) Then ReDim Preserve IMEI(31)
    8.    
    9.     For A = 0 To 30 Step 2
    10.         If IMEI(A) > 47 Then IMEI(A) = IMEI(A) - 48
    11.     Next A
    12.    
    13.     B = IMEI(0) Xor IMEI(2) Xor IMEI(4) Xor IMEI(6) Xor IMEI(8) Xor IMEI(10) Xor IMEI(12) Xor IMEI(14) Xor IMEI(16) Xor IMEI(18) Xor IMEI(20) Xor IMEI(22) Xor IMEI(24) Xor IMEI(26) Xor IMEI(28) Xor IMEI(30)
    14.    
    15.     Select Case CodeMode
    16.         Case False
    17.             Code(0) = B + 48
    18.             Code(2) = (B Xor 3) + 48
    19.             Code(4) = (B Xor 6) + 48
    20.             Code(6) = (B Xor 5) + 48
    21.             Code(8) = (B Xor 5) + 48
    22.             Code(10) = (B Xor 6) + 48
    23.             Code(12) = (B Xor 10) + 48
    24.             Code(14) = (B Xor 5) + 48
    25.         Case True
    26.             Code(0) = (B Xor 11) + 48
    27.             Code(2) = (B Xor 3) + 48
    28.             Code(4) = (B Xor 1) + 48
    29.             Code(6) = (B Xor 3) + 48
    30.             Code(8) = (B Xor 7) + 48
    31.             Code(10) = (B Xor 3) + 48
    32.             Code(12) = (B Xor 4) + 48
    33.             Code(14) = (B Xor 1) + 48
    34.     End Select
    35.    
    36.     GetCode = Code
    37. End Function

    Which gives a correct result to the only example you gave. But for other input it gives all wrong results (I'm 100% sure about this, nobody can write that complex code to do such a simple thing as this code above does).

    In the other hand, coding an alternative might just end up near impossible. In which code you should just make a VB compatible DLL and call the C code that way.

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2003
    Posts
    43
    well mates. it is not working very well yet.
    but here goes some more valid codes:

    000000000000000
    04840604
    59575151

    111111111111119
    22995885
    11010511

    222222222222228
    70099570
    68778676

    333333333333337
    68966588
    19131299


    thanks mates
    This forum rocks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width