char * to bool

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Travis

    #1

    char * to bool

    I'm curious what the most reliable way to convert a char * (or a char
    for that matter) to a bool is.

    It's safe to assume that char * will be either '0' and or '1' and I
    want the bool set accordingly.

    I'm seeing stuff (from others) like this :

    bool myBool = (char)atoi(myCh arPtr);

  • Travis

    #2
    Re: char * to bool

    On Jul 12, 4:59 pm, Travis <travis.bow...@ gmail.comwrote:
    I'm curious what the most reliable way to convert a char * (or a char
    for that matter) to a bool is.
    >
    It's safe to assume that char * will be either '0' and or '1' and I
    want the bool set accordingly.
    >
    I'm seeing stuff (from others) like this :
    >
    bool myBool = (char)atoi(myCh arPtr);
    Also to be noted, I also see

    bool myBool = !( (char)atoi(myCh arPtr) );

    I know ! is a bitwise NOT so how does that work against a char? It's
    flipping all the bits and then the bool is determined how? Based on
    the value of the lowest bit?

    Comment

    • David Harmon

      #3
      Re: char * to bool

      On Thu, 12 Jul 2007 23:59:06 -0000 in comp.lang.c++, Travis
      <travis.bowers@ gmail.comwrote,
      >I'm curious what the most reliable way to convert a char * (or a char
      >for that matter) to a bool is.
      All methods that work are equally reliable, no?
      >It's safe to assume that char * will be either '0' and or '1' and I
      >want the bool set accordingly.
      bool result = the_char != '0';
      bool result = *char_ptr != '0';
      >I'm seeing stuff (from others) like this :
      >
      >bool myBool = (char)atoi(myCh arPtr);
      Insane.

      Comment

      • Robert Bauck Hamar

        #4
        Re: char * to bool

        Travis wrote:
        On Jul 12, 4:59 pm, Travis <travis.bow...@ gmail.comwrote:
        >I'm curious what the most reliable way to convert a char * (or a char
        >for that matter) to a bool is.
        Depends on what you want.
        >It's safe to assume that char * will be either '0' and or '1' and I
        >want the bool set accordingly.
        You mean that the char pointed to is '0' or '1'? Like

        char a = '0';
        char *p = &a;

        bool b = *p != '0';

        b will be true if a is not '0', and false if a == '0'.
        >I'm seeing stuff (from others) like this :
        >>
        >bool myBool = (char)atoi(myCh arPtr);
        atoi tries to convert a sequence of numeric characters in a C style string
        to an int. The cast transforms this result to a char. bool would be true if
        the value returned fits into a bool and is nonzero or there is no numbers
        at the beginning of the string (except whitespace). It would be false if
        the value is zero, and you would have a bug if it doesn't fit into a char.
        Also to be noted, I also see
        >
        bool myBool = !( (char)atoi(myCh arPtr) );
        >
        I know ! is a bitwise NOT so how does that work against a char?
        No, it's a boolean not. !true == false, and !false == true. A char is
        nothing more than an integer in memory, so a value different from 0
        converts to true, and a 0 converts to false.
        It's
        flipping all the bits and then the bool is determined how? Based on
        the value of the lowest bit?
        No. ! converts its operand to a bool by the rule I sketched above, and then
        flips the truth value.

        --
        rbh

        Comment

        • Zachary  Turner

          #5
          Re: char * to bool

          On Jul 12, 7:00 pm, Travis <travis.bow...@ gmail.comwrote:
          bool myBool = !( (char)atoi(myCh arPtr) );
          >
          I know ! is a bitwise NOT so how does that work against a char? It's
          flipping all the bits and then the bool is determined how? Based on
          the value of the lowest bit?
          You're thinking of ~ (tilde). ! is a logical not. !0 = true, and !
          anything else = false

          Comment

          • Travis

            #6
            Re: char * to bool

            On Jul 12, 8:19 pm, Zachary Turner <divisorthe...@ gmail.comwrote:
            On Jul 12, 7:00 pm, Travis <travis.bow...@ gmail.comwrote:
            >
            bool myBool = !( (char)atoi(myCh arPtr) );
            >
            I know ! is a bitwise NOT so how does that work against a char? It's
            flipping all the bits and then the bool is determined how? Based on
            the value of the lowest bit?
            >
            You're thinking of ~ (tilde). ! is a logical not. !0 = true, and !
            anything else = false
            Ah you're right. Thanks.

            Doing something like bool myBool = myChar != '0' makes sense.

            Thanks everyone.

            Comment

            • Zachary  Turner

              #7
              Re: char * to bool

              On Jul 13, 11:09 am, Travis <travis.bow...@ gmail.comwrote:
              On Jul 12, 8:19 pm, Zachary Turner <divisorthe...@ gmail.comwrote:
              >
              On Jul 12, 7:00 pm, Travis <travis.bow...@ gmail.comwrote:
              >
              bool myBool = !( (char)atoi(myCh arPtr) );
              >
              I know ! is a bitwise NOT so how does that work against a char? It's
              flipping all the bits and then the bool is determined how? Based on
              the value of the lowest bit?
              >
              You're thinking of ~ (tilde). ! is a logical not. !0 = true, and !
              anything else = false
              >
              Ah you're right. Thanks.
              >
              Doing something like bool myBool = myChar != '0' makes sense.
              >
              Thanks everyone.
              Yes, but please use parentehses :(

              Comment

              Working...