Results 1 to 3 of 3

Thread: casting char to int

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    92

    casting char to int

    Hi !

    I use the following to read a token :

    char delimiter[] = " ";
    char *token;


    token = strtok( line, delimiter);
    while( token != NULL ) {

    printf( " %s\n", token );
    token = strtok( NULL, delimiter );
    }

    i want to cast the toket that i read to integer and insert it to array.

    anyone ?

  2. #2
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    Are you tokenising characters or strings? Either way you are generating strings (char arrays), and therefore casting to int would give you the pointer value (I think - correct me if I'm wrong).

    If you know that the token is going to be a single char then you could use:

    int num = (int)token[0];

    Perhaps..?

    HD

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Use itoa() to convert from const char* to int.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

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



Click Here to Expand Forum to Full Width