Thread: Need help understanding and simplifying code!

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    2

    Need help understanding and simplifying code!

    So I have this challenge where I must first, understand what the code is doing then re-write it to basically make it more efficient. This is not homework nor a graded assignment. I'm trying to understand C because I only learned JAVA in school so I'm pretty unfamiliar with C. I would greatly appreciate any help!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <sys/time.h>
    typedef int int32;
    typedef unsigned char int8;
    static void combine_it(int8  *output, int8  *input_1, const int8  *input_2, int32 length)
    {
        int32 i = 0;
        for(i=0; i<length; i++)
        {
            output[i] = (int8)(input_1[i] ^ input_2[i]);
        }
        return;
    }
    static void itoa( int32 num, int8  *alpha, int32 radix )
    {
        if( radix == 10 )
        {
            sprintf(alpha, "%i", num);
        }
        else if( radix == 16 )
        {
            sprintf(alpha, "%X", num);
        }
    }
    int8 *create_new_key(int8 modifier, const int8  *input_1, int32 length)
    {
        int8  leading[3];
        int32 i_leading;
        int8 * temp_string = NULL;
        int8 * ret;
        int32 i = 0;
        itoa(modifier/2, leading, 10);
        i_leading = atoi(leading);
        temp_string = (int8 *) malloc(8);
        ret = (int8 *) malloc(length);
        memset(temp_string, 0, 8);
        temp_string[0] = 0; 
        if( (modifier+1)%2 == 0 ) {
            temp_string[0] = (int8)((i_leading<<4) + 8);
        }   
        else {
            temp_string[0] = (int8)(i_leading<<4);
        }   
        for(i=0; i<(length>>3); i++)
        {
            combine_it(ret+i*8, temp_string, input_1+i*8, 8);
        }   
        free(temp_string);
        return ret;
    }   
    // Do not change anything in main
    // main only contains example input
    // Change anything else
    int main(int argc, char **argv) {
        // Example input data
        int8 data[32] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,
                         17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};
        int8 *resp = create_new_key(0x10, data, sizeof(data));
        free(resp);
        return 0;
    }
    Last edited by NewDay; 08-06-2015 at 11:07 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,722
    What do you understand of the code, and which parts do you need help to understand?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    If you are unfamiliar with C, I would suggest putting aside this "challenge" until you become more familiar with the basics.

  4. #4
    Registered User
    Join Date
    Aug 2015
    Posts
    2
    Quote Originally Posted by laserlight View Post
    What do you understand of the code, and which parts do you need help to understand?
    I understand that the main is an array that is randomly selecting 32 characters for a password generator I believe. I just don't understand what static void combine_it and static void itoa are doing. Can you help me understand please

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Pick one to start. Let's look at "itoa()" first (note that this is the name of a non-standard function that some compilers support - if your compiler does support it, then you should be getting an error when you compile).

    Looking at the term "radix", and the numbers 10 and 16 - does this give you any clues as to what the function might be doing?

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    3,824
    Code:
    temp_string[0] = (int8)((i_leading<<4) + 8);
    You need to understand C type casts; the "(int8)" is a C type cast.

    But, before that I would work on understanding atoi and itoa functions.
    As was suggested in the prior post.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Understanding This Code.
    By thisbeme in forum C Programming
    Replies: 11
    Last Post: 03-04-2011, 12:01 PM
  2. Need some help understanding the following code
    By GTTDi in forum C Programming
    Replies: 1
    Last Post: 10-15-2010, 07:28 AM
  3. Simplifying Code
    By Myst1caL in forum C Programming
    Replies: 6
    Last Post: 09-08-2007, 03:57 PM
  4. Simplifying code
    By Taka in forum C Programming
    Replies: 1
    Last Post: 10-22-2006, 02:12 AM
  5. help understanding code
    By volk in forum C Programming
    Replies: 5
    Last Post: 02-08-2003, 10:50 AM