C++ question ( char* to int )

Discussion in 'OT Technology' started by A Cow, Apr 30, 2007.

  1. A Cow

    A Cow OT Supporter

    Joined:
    Jul 7, 2005
    Messages:
    33,038
    Likes Received:
    97
    Location:
    Vancouver BC
    hey.. heres the story.. im doing a networking program where it gets the message ( DataBuffer[1024] ) and compairs to it a int ( 1, 2, 3 etc ) but i cant figure out how to convert from a char* to a int.. any help / ideas?
    char DataBuffer[1024];

    // set DataBuffer to value from the interwebs here

    if ( DataBuffer == '1' )
    {
    do shit;
    }
    else if ( DataBuffer == '2' )
    {
    do more shit;
    }
     
  2. Krakerjak

    Krakerjak Active Member

    Joined:
    Jul 7, 2003
    Messages:
    8,288
    Likes Received:
    0
    Location:
    Edmonton eh
    Look up typecasting
     
  3. A Cow

    A Cow OT Supporter

    Joined:
    Jul 7, 2005
    Messages:
    33,038
    Likes Received:
    97
    Location:
    Vancouver BC
    aye.. i tried that already.. but what it was was the single ' instead of double "

    now its just not going into the if statement.. any ideas..

    ( i send message 1 and it skips over it .. i send message 2 and it also skips over 2 )
     
  4. A Cow

    A Cow OT Supporter

    Joined:
    Jul 7, 2005
    Messages:
    33,038
    Likes Received:
    97
    Location:
    Vancouver BC
    btw does anyone know how to to do the bitwize ==?

    thanks..
     
  5. Ractoon

    Ractoon Shibumi in progress...

    Joined:
    Apr 30, 2001
    Messages:
    256
    Likes Received:
    0
    I may be missing something here, but couldn't you use atoi()? Also, for some reason I'm thinking bitwise compare is |=
     
  6. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    read the man page for strtol and friends
     
  7. A Cow

    A Cow OT Supporter

    Joined:
    Jul 7, 2005
    Messages:
    33,038
    Likes Received:
    97
    Location:
    Vancouver BC
    i feel stupid thianks.. ( i was trying to use old sko casting.. (int)DataBuffer )
     
  8. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    (int)(*(DataBuffer[0])) would probably work
     
  9. StevesVR4

    StevesVR4 Get Arrested

    Joined:
    Jul 1, 2003
    Messages:
    7,328
    Likes Received:
    0
    That will work from the compiler's perspective, but it won't provide the result you are looking for. The compiler will treat the first four bytes in the array as an integer and use that. The result will not be what you are expecting.
    As an example:
    Code:
    char DataBuffer[1024] = "735624";
    int number = (int)(*(DataBuffer[0]));
    cout << number << endl;
    
    When you output the integer, you would expect to see 735624 but you actually would see 926102838. What happens is the memory assigned to DataBuffer contains the hex values 37 33 35 36 32 34 00 and when you attempt to do the cast, the compiler takes the first four bytes and treats it like an integer, thus the number is 0x37333536.
     

Share This Page