Thread: VB to C++ problem

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    7

    VB to C++ problem

    Hello, im trying to port some vb code into C++, and ive run into a snag.

    I took the following code in vb:

    Code:
    For Index = 0 To 71
    	T1 = (T1 + TempCard(TempCard(Index) Mod 17) + TempCard(Index)) And &HFF&
    	TempCard(Index) = T1
    Next Index
    And Rewrote it in C++ to get:

    Code:
    for (unsigned int n = 0; n < 72; n++){
    	T1 = (T1 + TempCard[TempCard[n] % 17] + TempCard[n]) & 0xFF;
    	TempCard[n] = T1;
    }
    I cant find anything wrong with the C++ code, however its returning different values in TempCard and T1 then the vb app. They are the same values going into the loop, but different coming out. Also, TempCard[72] is an integer and T1 is also an integer. Any help would be appreciated.

    Mike

    Edit: typo
    Last edited by Mike_01; 04-04-2004 at 02:46 PM.

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    And here are the values before and after the loops if this helps:

    Before:
    Code:
    TempCard[] = {2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139}
    
    T1 = 0
    After in vb (values i want):
    Code:
    TempCard[] = {204, 60, 172, 96, 147, 118, 112, 114, 89, 167, 217, 164, 97, 209, 208, 161, 243, 56, 21, 111, 114, 101, 51, 156, 87, 85, 60, 111, 182, 165, 91, 70, 196, 75, 158, 110, 205, 38, 212, 158, 229, 249, 13, 41, 146, 23, 197, 133, 10, 100, 231, 230, 25, 195, 200, 46, 110, 110, 0, 89, 109, 94, 208, 6, 75, 67, 13, 78, 83, 28, 16, 218}
    
    T1 = 218
    After in c(wrong values):
    Code:
    TempCard[] = {139, 185, 67, 206, 19, 108, 154, 36, 175, 244, 77, 254, 136, 19, 88, 177, 98, 236, 119, 188, 21, 198, 80, 219, 32, 121, 42, 180, 63, 132, 221, 142, 24, 163, 232, 65, 242, 124, 7, 76, 165, 86, 224, 107, 176, 9, 186, 68, 207, 20, 109, 30, 168, 51, 120, 209, 130, 12, 151, 220, 53, 230, 112, 251, 64, 153, 74, 212, 95, 164, 253, 48}
    
    T1 = 48

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Looks fine to me.
    Double check that the initial values for TempCard[] are identical.

    gg

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    I have, and they are. Im about to go thru this loop on paper to see if I can figure out how its screwing up that way. Should take a while, LOL. I've also tried breaking it down to one operation per line to eliminate the problem being order of operations, but its not that.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    Ok, this is odd.

    When I check the values of TempCard using this method:
    Code:
    for (unsigned int n = 0; n < 72; n++){
    	T1 = (T1 + TempCard[TempCard[n] % 17] + TempCard[n]) & 0xFF;
    	TempCard[n] = T1;
    	cout << TempCard[n] << endl;
    }
    TempCard[71] = 48

    However, when I check it using this method:
    Code:
    for (unsigned int n = 0; n < 72; n++){
    	T1 = (T1 + TempCard[TempCard[n] % 17] + TempCard[n]) & 0xFF;
    	TempCard[n] = T1;
    }
    for (unsigned int n = 0; n < 72; n++){
    	cout << TempCard[n] << endl;
    }
    TempCard[71] = 27

    All the other values are the same both ways.

    Is it possible my cout (which im using to check the values) is messing with the values somehow, or somehow the loop is?

    Edit: added above question

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Is T1 initialized to the same value in both cases?
    What datatype is your TempCard arrray?

    My results are different from all the results:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        unsigned char TempCard[] = 
            {2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 
             139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 
             23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 
             255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 0, 2, 139, 23, 255, 
             0, 2, 139, 23, 255, 0, 2, 139};
    
        unsigned int T1 = 0;
        unsigned int n;
    
        for (n = 0; n < 72; n++)
        {
            T1 = (T1 + TempCard[TempCard[n] % 17] + TempCard[n]) & 0xFF;
            TempCard[n] = T1;
        }//for
    
        
        for (n = 0; n < 72; n++)
            cout << (unsigned int)TempCard[n] << ", ";
        
        cout << endl << T1 << endl;
    
        return 0;
    }//main
    
    /* Output:
    25, 163, 69, 93, 118, 189, 165, 97, 121, 146, 217, 193, 125, 149, 174, 245, 
    221, 153, 177, 202, 17, 249, 181, 205, 230, 45, 21, 209, 233, 2, 73, 49, 237, 
    5, 30, 101, 77, 9, 33, 58, 129, 105, 37, 61, 86, 157, 133, 65, 89, 114, 185, 
    161, 93, 117, 142, 213, 189, 121, 145, 170, 241, 217, 149, 173, 198, 13, 245, 
    177, 201, 226, 41, 17,
    17
    */
    gg

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    T1 and TempCard are both int's. T1 = 0 before both loops. All var's are the same before the loops.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @Mike_01
    Post something compilable with output (like Codeplug did)
    What OS/compiler are you using?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    OK, the following code is cut and pasted together from my larger project so there might be a few declarations or includes that dont need to be here.

    Code:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <math.h>
    
    using namespace std;
    
    int HEX_TO_DEC(string HEX_VALUE)
    {
        int DEC_VALUE;
    	sscanf(HEX_VALUE.c_str(), "%x", &DEC_VALUE);
        return DEC_VALUE;
    }
    
    int main()
    {
    	int T1 = 0;
    	int TempCard[72];
    	string temp;
    	string Card = "028B17FF00";
    	for (int n=1; n<15; n++) { temp += Card; }
    
    	temp += Card.substr(0,4);
    	cout << "TempCard Prior To Loop" << endl;
    	for (unsigned int n=0; n < temp.length(); n += 2){
    		TempCard [(n - 1) / 2] = HEX_TO_DEC(temp.substr(n, 2));
    		cout << TempCard [(n - 1) / 2] << endl;
    	}
    	// At this point, TempCard is good.
    	cout << "TempCard After Loop" << endl;
    	for (unsigned int n = 0; n < 72; n++){
    		T1 = (T1 + TempCard[TempCard[n] % 17] + TempCard[n]) & 0xFF;
    		TempCard[n] = T1;
    		cout << TempCard[n] << endl;
    	}
    	//Now The Values in TempCard are not what they should be.
    	system("PAUSE");
    	return 0;
    
    
    }
    Im using VC++ 7 (VS.NET) as my complier. OS is XP Pro. Although ive tried the same loop in gcc on linux with the same problem.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Now give us something to compare it to.....

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    	for (unsigned int n=0; n < temp.length(); n += 2){
    		TempCard [(n - 1) / 2] = HEX_TO_DEC(temp.substr(n, 2));
    		cout << TempCard [(n - 1) / 2] << endl;
    	}
    This doesn't work too well for me. I'd suggest simplifying to
    Code:
      for (unsigned int n = 0, i = 0; n < temp.length(); n += 2, i++)
      {
        TempCard[i] = HEX_TO_DEC(temp.substr(n, 2));
        cout << TempCard[i] << endl;
      }
    To prove the array is good, use this
    Code:
      for (unsigned int n = 0; n < 72; n++)
      {
        cout << TempCard[n] << ", ";
      }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, let's analyse

    Before entering that last for loop section, the start of the TempCard array will be like so:
    Code:
     Element 0: 2
     Element 1: 139
     Element 2: 23
     ...
    This is as per your previous post, and seems to be so in a slightly modified C++ coded version.

    This line of code sets TempCard[n] in the last for loop
    Code:
     T1 = (T1 + TempCard[TempCard[n] % 17] + TempCard[n]) & 0xFF;
    The first time through that loop, n will be 0. We know all other values, so a simply breakdown of this line will tell us the value of TempCard[0], which will be set at the end of the first iteration.
    Code:
     T1 = (T1 + TempCard[TempCard[n] % 17] + TempCard[n]) & 0xFF;
        = (0  + 23                         + 2          ) & 0xFF = 25
    So, TempCard[0] = 25.

    I don't know why VB is giving you 204.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    Yes Hammer, exactly correct. I cant remember how I got that output from vb, but it is incorrect. Codeplugs output was the correct one. Of course this still wasnt the output I was getting in C++.

    I figured the problem out, and it was my first loop which was setting TempCard[0] and TempCard[1] to the same value. It seems VB rounds numbers differently (or at least in the way my code was written). The only problem is that C++ was doing it "properly". After modifying the first loop the values are all correct now.

    What I cant understand is where I got those very odd values from in VB earlier, and how I got the correct output in my first loop in C++ for the first TempCard element when it was not correct. The problem most likely has something to do with the fact that when I was first checking these numbers I was outputting them from inside the problem loops, not after the loops had finished.

    Anyways, I would like to thank you guys for the time and effort you put into helping me with this. I am very grateful.

    Mike

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Sending a string to C++ from VB 6.
    By VirtualAce in forum C++ Programming
    Replies: 4
    Last Post: 08-21-2001, 02:28 AM