Results 1 to 13 of 13

Thread: C++ Programming Question

  1. #1
    BGI 5x
    Guest

    C++ Programming Question

    Hi, im coding in c++ but i found a problem i cant get a thing to work

    i need to get the len of the serial if its less than 10 i add a "9" to the start of it, this was what i could do

    length1=strlen(heh);
    while (length1 != 10)
    {
    heh[length1]+=0x39;
    length1++;
    }

    but this only add one "9" at the end of the string, the rest is filled with shit, if anyone can help.

    tnx
    BGI 5x
    I promise that I have read the FAQ and tried to use the Search to answer my question.

  2. #2
    Hiya BGI 5X,

    From your description, I'm not entirely sure what the serial algo is but I can explain why your code isn't working.

    First your while statement...
    If you're dealing with a condition where the serial length really must be "less than" 10, using the "not equals" operator is ambiguous and fails to correctly describe the situation. Whether the length is 1 or 100 it still remains != 10.

    while(length1 != 10)
    should be
    while(length1 < 10)

    Next, look at heh[length1] += 0x39;
    length1++;

    This explains your problem of 1 '9' at the end of the string and shit in the rest of it.

    Length1, being the length of the string, will start to index the heh array at the last element of the string (which explains the '9'at the end of the string). Then, presumably because the array size of heh is longer than the actual string length, length1++ proceeds to index past the last element in the string into the remainder of the array. If you did not initialize the contents of the array when you delcared it, you will now be adding 0x39 to whatever crap is already in those memory locations beyond your string resulting in the shit of which you speak. I should also mention that if your serial length were to be greater than 10, the length1 variable would never == 10 and terminate the while loop... Infinite Loop == crash

    From your description, I'm not entirely sure of the serial algo. Are you adding the ascii value for 9 (0x39) to the existing ascii values of each element in the string or are you simply adding 9 (0x09)to the existing ascii values for the string elements. For the sake of example I'm going to assume you're adding 0x39. Also for this situation I would suggest a 'for' loop rather than a 'while' loop.

    Example: If the serial length is less than 10 add 0x39 to each element in the string starting with the first element.

    length1=strlen(heh);
    if(length1 < 10)
    {
    for(int i=0; i<length1; i++)
    heh[i]+=0x39;
    }

    Anyway, I hope this clears up a few of the bugs If you post more details of the algorithm you're trying to code, I'm sure either myself or someone else around here can help you with your C.

    Regards,
    Clandestiny

  3. #3
    m0sk
    Guest
    hmmm :-)

    I don't know sh*t about C++, but my guess is you confuse C++ strings with C-style strings (character arrays actually)... If you declared heh as a char* or char[] (which seems to be the case, since you're using strlen()), then AFAIK, the snippet you gave is C, not C++. So the += operator is the plus assignment, not the concat operator ;-) That's why you see these strange things, explained by Clandestiny

    Use concat() with c-style strings, and += with the C++ datatype string, but don't mix them...
    I promise that I have read the FAQ and tried to use the Search to answer my question.

  4. #4
    m0sk
    Guest
    hmmm :-)

    I don't know sh*t about C++, but my guess is you confuse C++ strings with C-style strings (character arrays actually)... If you declared heh as a char* or char[] (which seems to be the case, since you're using strlen()), then AFAIK, the snippet you gave is C, not C++. So the += operator is the plus assignment, not the concat operator ;-) That's why you see these strange things, explained by Clandestiny

    Use concat() with c-style strings, and += with the C++ datatype string, but don't mix them...
    I promise that I have read the FAQ and tried to use the Search to answer my question.

  5. #5
    BGI 5x
    Guest
    Hello All

    here is a resume

    the program makes a checksum with name this checksum is X chars
    suppose X = 6 chars (ABCDEF)
    then the program will add a 0x39 at the start of ABCDEF until it gets 10
    so in this case it'll be 9999ABCDEF

    Clandestiny, I tried using your code and it didnt work, then i debugged it and say that it was adding 0x39 to the already made checksum :/

    maybe this is coz what m0sk said about heh[i]+

    btw Im using visual c++ 6.0.

    if you didnt understand the algo i can explain again.

    thanks
    BGI 5x
    I promise that I have read the FAQ and tried to use the Search to answer my question.

  6. #6
    Wizard
    Guest
    non-optimised 'C' program :

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    char szSerial[50];
    char szBuffer[50];

    printf("Serial : ");
    gets(szSerial);

    while (strlen(szSerial)<10)
    {
    strcpy(szBuffer,"9");
    strcat(szBuffer,szSerial);
    strcpy(szSerial,szBuffer);
    }

    printf("New serial = %s",szSerial);
    getchar();

    return 0;
    }


    Wizard
    I promise that I have read the FAQ and tried to use the Search to answer my question.

  7. #7
    BGI 5x
    Guest
    yes, thats it Wizard, thanks a lot.
    tnx all that helped
    I promise that I have read the FAQ and tried to use the Search to answer my question.

  8. #8
    anon
    Guest
    Two Lines:

    for(int i=0; i<(10-OldSerialLength);NewSerial[i++]);
    for(int j=0; NewSerial[i++]=OldSerial[j++];
    I promise that I have read the FAQ and tried to use the Search to answer my question.

  9. #9
    anon
    Guest
    anon (08-27-2001 22:14):
    Two Lines:

    for(int i=0; i<(10-OldSerialLength);NewSerial[i++]);
    for(int j=0; NewSerial[i++]=OldSerial[j++];
    oops last statement in first for loop needs to be

    " NewSerial[i++]='9' "
    I promise that I have read the FAQ and tried to use the Search to answer my question.

  10. #10
    Wizard
    Guest
    for(int i=0; i<(10-OldSerialLength);NewSerial[i++]='9');
    for(int j=0; j<OldSerialLength+1;NewSerial[i++]=OldSerial[j++]);

    ... would be better

    Wizard
    I promise that I have read the FAQ and tried to use the Search to answer my question.

  11. #11
    FatHead_Slim
    Guest
    Were did my "i" go?

    need to declare i outside of first loop cause its referanced in second

    int i=0;
    while( i<(10-OldSerialLength);NewSerial[i++]='9');
    for(int j=0; j<OldSerialLength+1;NewSerial[i++]=OldSerial[j++]);
    I promise that I have read the FAQ and tried to use the Search to answer my question.

  12. #12
    qferret
    Guest
    ...same routine...loop 2 is within loop 1 ....i is considered local for both loops (nested for loops)
    I promise that I have read the FAQ and tried to use the Search to answer my question.

  13. #13
    Wizard
    Guest
    "need to declare i outside of first loop cause its referanced in second"

    Wrong, you don't *need* to : it depends on your compiler settings.

    Anon (corrected) code perfectly works with VC++ default settings.

    Wizard
    I promise that I have read the FAQ and tried to use the Search to answer my question.

Similar Threads

  1. Programming Loader under MAC -> API Calls
    By Drigo in forum The Newbie Forum
    Replies: 6
    Last Post: September 1st, 2009, 21:10
  2. Reversing & Programming in Germany
    By Ntoskrnl in forum Off Topic
    Replies: 7
    Last Post: August 11th, 2006, 11:48
  3. A New RCE / Programming Forum
    By Khaosgott96 in forum Off Topic
    Replies: 1
    Last Post: August 30th, 2005, 12:30
  4. Any Assembly Programming Certification?
    By profdracula in forum Off Topic
    Replies: 3
    Last Post: August 1st, 2004, 23:56
  5. Programming Problems
    By BGI 5x in forum Malware Analysis and Unpacking Forum
    Replies: 4
    Last Post: August 25th, 2001, 09:58

Bookmarks

Posting Permissions

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