String, null-termination and/or strlen issue

This is a discussion on String, null-termination and/or strlen issue within the C Programming forums, part of the General Programming Boards category; Hello all. This is definitly the best site for help on c projects. ( Looking at Adak, Laserlight, Grumpy ....) ...

  1. #1
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294

    String, null-termination and/or strlen issue

    Hello all. This is definitly the best site for help on c projects. ( Looking at Adak, Laserlight, Grumpy ....) The program description is in the comment at the head of the source code. Basically it converts any integer from base 10 into a base of the user's choosing. Im having a problem at line 54 when I set variable "J" to "strlen( s ) -1". It returns -1, which means its reading my string as zero. Any suggestions?

    Code:
    /* Write a function itob(n,s,b) that converts the integer
     * n into a base b character representation in the string
     * s. In particular, itob(n,s,16) formats n as a hexadecimal
     * integer in s.
     */
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void itob( int , char * , int );
    int strtoi( char * );
    void reverse( char * );
    char * find_space( int , int );
    
    int main( int argc , char **argv ){
        
        if( argc != 3 )
            /* expects two arguments, num and new base */
            printf( "Usage: %s n b\nConvert base-10 number [n] into base-[b] number\n" , argv[ 0 ] );
        else{
            int n , b;
            char * string = NULL;
            if( !( n = strtoi( argv[ 1 ] ) ) || !( b = strtoi( argv[ 2 ] ) ) )
                puts( "Invalid integer(s) has been detected...abort" );
            else{
                string = find_space( n , b );
                itob( n , string , b );
                printf( "%s\n" , string );
                getchar();
            }
        }
        
        return 0;
    }
    int strtoi( char * str ){
        int n , i;
        for( i = 0 , n = 0;str[ i ] != '\0' && str[ i ] <= '9' && str[ i ] >= '0' ; ++i )
            n = n * 10 + ( str[ i ] - '0' );
        return ( str[ i ] == '\0' ) ? n : 0;
    }
    void itob( int n , char * string , int b ){
        int i , r;
        for( i = 0;n > 0; ++i ){
            string[ i ] = ( r = n % b ) ;
            n /= b;
        }
        string[ i ] = '\0';
        reverse( string );
        return;
    }
    void reverse( char * s ){    // taken from K&R, pg 63.
        int c , i , j;
        for( i = 0 , j = strlen( s ) - 1; i < j; ++i , --j )
            c = s[ i ] , s[ i ] = s[ j ] , s[ j ] = c;
        return;
    }
    char * find_space( int n , int b ){
        int i;
        char * string;
        for( i = 1;n > 0;++i )        // EDIT: changed i = 0    to    i = 1 , counting , not using array indexing
            n /= b;
        string = malloc( ( sizeof( char ) * i ) + 1 );
        return string;
    }
    EDIT: I know i dont free my malloc string, not worried about that right now
    Last edited by jwroblewski44; 07-26-2012 at 01:34 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,629
    What input are you giving it? I assume it's something that ends in zero in your new base, e.g. 32 in base 10 becomes 20 in base 16. Your problem is on line 45, you are assigning the numeric value, not the ASCII value to string[i]. That means you put a zero (null character) in string[0], instead of an ASCII '0'.

  3. #3
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Ahhhh yes, thank you very much Andreas!

    Code:
    ...
    for( i = 0;n > 0; ++i ){
            r = ( n % b ) + '0' , string[ i ] = r;
            n /= b;
        }
        string[ i ] = '\0';

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,629
    It's anduril, not Andreas, but no biggie.

    One other thing, why do you make such heavy use of the comma? You can use it in a for loop if you have two things to initialize or increment. Outside of that, and the obvious function parameter, you should use it sparingly. Put statements on their own line and use a semicolon to separate them. Avoid cramming too much stuff into one line/statement in general. It makes your code much easier to read (i.e. you'll make fewer bugs, and it will be easier for you and us to find and fix them).
    Last edited by anduril462; 07-26-2012 at 04:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string with different strlen's
    By jeanluca in forum C Programming
    Replies: 6
    Last Post: 03-29-2010, 01:05 AM
  2. null termination with str(n)cpy and strlen
    By jeanluca in forum C Programming
    Replies: 9
    Last Post: 06-22-2009, 04:52 PM
  3. Quick Null Termination Question
    By tommyb05 in forum C Programming
    Replies: 9
    Last Post: 06-05-2009, 10:35 AM
  4. Can't save file! [NULL Byte termination]
    By q6z4k in forum Networking/Device Communication
    Replies: 8
    Last Post: 07-13-2005, 05:45 PM
  5. null termination: seemingly unnecessary
    By krygen in forum C++ Programming
    Replies: 7
    Last Post: 11-08-2004, 11:47 PM

Tags for this Thread


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