Thread: intgers form a string

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    13

    intgers form a string

    All right, here it goes. I'm trying to get an intger from a string. FOr example I don't want "this" to be 4 intgers, I want to get AN intger from "this" Is there any easy way to do this?
    #define punkCow

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What do you mean by "this"
    Store the 4 characters in an int in some way?

    Say
    Code:
    int floogle = 't'<<24 | 'h' << 16 | 'i' << 8 | 's'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    King of the Internet Fahrenheit's Avatar
    Join Date
    Oct 2001
    Posts
    128
    int whatevar = (int) 'this' ;

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >int whatevar = (int) 'this' ;
    And this is supposed to be meaningful, how?

    >I'm trying to get an intger from a string.
    Be more specific. You can get an integer from a string many different ways, it depends on what you want to do with it.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    27
    Hi,

    Do you mean something like this?
    Code:
    union aryint
    {
         char chrs[4];
         int    x;
    };
    union aryint ai;

    ai.chrs[0] = 't';
    ai.chrs[1] = 'h';
    and so on.

    Then

    printf("The int is %d",ai.x);


    Is this what you want?
    Pappy
    You learn something new everyday.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Another wild attempt at answering the non-specific question:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM