Xor problems

This is a discussion on Xor problems within the C++ Programming forums, part of the General Programming Boards category; Hello Code: void KeyGen (char* UserName) { int length; char * b_name; int divider=10; int counter; char tmp; int tmp_int; ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    36

    Xor problems

    Hello
    Code:
    void KeyGen (char* UserName)
    {
     int length;
     char * b_name;
     int divider=10;
     int counter;
     char tmp;
     int tmp_int;
     float tot;
     double fract;
     double integer;
     int Serial[10];
     char p_hex[0];
     char * Serial_out;
     int i_integer;
     char tot_hex='0';
     char *FullSerial;
     if(strlen(UserName)>11)
      {
        b_name=new char[strlen(UserName)];
        strxfrm(b_name,UserName,10);
       } 
     else
      {
       b_name=new char[strlen(UserName)];
       strcpy(b_name,UserName);
      }
      length=strlen(b_name); 
      cout << endl;
     
    
    for(counter=0;counter<length;counter++)
       {
         tmp=b_name[counter];
         tmp_int=(int)tmp;
         itoa(tmp_int,p_hex,16);
         tot=(float)tmp_int/10;
         fract=modf(tot,&integer);
         fract=fract*10;
         fract=((int)fract^counter);<--------------
         cout << fract << endl;
        fract=fract+2;
        
        if(fract>=10)
         {
          fract=fract-10;
         }
         
        itoa((int)fract,p_hex,16);
         Serial[counter]=(int)fract;
        }
    
       cout << "Serial Number : " <<endl;
     
       for(int i=9;i>=0;i--)
        {
         cout << "0" <<Serial[i]<<endl;
        } 
        
       delete b_name;  
    }
    There is a problem with the line " fract=((int)fract^counter);"
    it doesn't always do the right thing...If i type Liquid][Shadow as UserName into the function above i'll get the following as Serial :

    02030807070307030607

    and this if i do it using Caclulator :

    05030807070306030608

    so for instance with letter "L",counter=0,fract=6 so
    fract=((int)fract^counter) would mean 6 xor 0 ,which is 6,
    but instead of 6 i get 5...why is this??

    if i pass the letter "l" into it,counter=0,fract=8 so
    fract=((int)fract^counter) would mean 8 xor 0,which is 8
    and this time my function does output 8

    i checked the output using cout << fract;

    can someone help me please thnx s


    Please use [code][/code]Tags

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Insert a breakpoint on the line, and check to make sure fract and counter ARE what you think they are.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I have 2 compiling problems with the code. p_hex array size is 0, I assume that is a typo, what size should it be? And I need the modf() function.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    36

    mhmm

    Even if they show correct values when i do cout << fract <<" " << counter << endl; ???
    Time for TOTAL WAR

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    36

    Ok here is the full code :

    Ithink u might need this first:

    #include <iostream>
    #include <stdlib.h>
    #include <windows.h>
    #include <wincon.h>
    #include <time.h>
    #include <string.h>

    void KeyGen (char* UserName)
    {
    int length;
    char * b_name;
    int divider=10;
    int counter;
    char tmp;
    int tmp_int;
    float tot;
    double fract;
    double integer;
    int Serial[10];
    char p_hex[0];
    char * Serial_out;
    int i_integer;
    char tot_hex='0';
    char *FullSerial;
    if(strlen(UserName)>11)
    {
    b_name=new char[strlen(UserName)];
    strxfrm(b_name,UserName,10);
    }
    else
    {
    b_name=new char[strlen(UserName)];
    strcpy(b_name,UserName);
    }
    length=strlen(b_name);
    cout << endl;


    for(counter=0;counter<length;counter++)
    {
    SetConsoleTextAttribute(Console_Handle,FOREGROUND_ BLUE | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    tmp=b_name[counter];
    cout << "----Character : " << tmp << endl;
    cout << " Counter : " << counter << endl;
    tmp_int=(int)tmp;
    itoa(tmp_int,p_hex,16);
    cout << " ASCII : " << tmp_int << endl;
    cout << " Hex : " << p_hex <<endl;
    tot=(float)tmp_int/10;
    cout <<" Div by 10 : "<< tot << endl;
    fract=modf(tot,&integer);
    fract=fract*10;
    cout <<" Integer : " << i_integer << endl;
    cout <<" Fractional : " << fract <<endl;
    fract=((int)fract^counter);
    cout <<" Fractional XOR Counter : " << fract <<endl;
    fract=fract+2;
    cout <<" Add 2 : " << fract << endl;
    if(fract>=10)
    {
    cout <<" Before -10 : " << fract << endl;
    fract=fract-10;
    cout <<" After -10 : " << fract << endl;
    }

    itoa((int)fract,p_hex,16);

    cout << " Result DEC/HEX : " << fract << " / " << p_hex <<endl;
    SetConsoleTextAttribute(Console_Handle,FOREGROUND_ GREEN|FOREGROUND_BLUE | FOREGROUND_INTENSITY);
    cout <<"--------------------------------------------------------------------------------" << endl;
    SetConsoleTextAttribute(Console_Handle,FOREGROUND_ BLUE | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    Serial[counter]=(int)fract;
    }

    cout << ":::Serial Number : " ;
    SetConsoleTextAttribute(Console_Handle,FOREGROUND_ GREEN|FOREGROUND_BLUE | FOREGROUND_INTENSITY);

    for(int i=9;i>=0;i--)
    {
    cout << "0" <<Serial[i];
    }

    delete b_name;
    }

    Thanks for helping me
    Time for TOTAL WAR

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    36

    absoultey

    You are absolutely right and i know it can be made much better...but the strange thing is still with the xor...and it seems that it isn't working with garbage even though my code is full of them.......thnx though
    Time for TOTAL WAR

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    36

    Thanks

    Thank u Salem...it was the rounding problem indeed...% solved it thanks again...how stupid of me...
    Time for TOTAL WAR

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21