Are C++ strings binary strings?

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

    #1

    Are C++ strings binary strings?

    Hi, can someone tell me if a C++ string (std::string) represents a
    binary or an ASCII string?

    Thanks in advance.

  • benben

    #2
    Re: Are C++ strings binary strings?

    ruffiano wrote:
    Hi, can someone tell me if a C++ string (std::string) represents a
    binary or an ASCII string?
    >
    Thanks in advance.
    >
    What is a binary string? If what you mean by binary is something that is
    stored in binary numbers then the answer to your question is both.

    Ben

    Comment

    • Victor Bazarov

      #3
      Re: Are C++ strings binary strings?

      ruffiano wrote:
      Hi, can someone tell me if a C++ string (std::string) represents a
      binary or an ASCII string?
      What's "a binary string"?

      It stores an array of 'char' values. That's all. 'ASCII' is a way
      to interpret a 'char' value as a printable character. Those two
      things are orthogonal. If you want to see an 'std::string' as
      a container of ASCII characters, power to you. On a different system
      somebody else might see it as a container of EBCDIC characters. Or
      a container of extended ASCII.

      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask


      Comment

      • loufoque

        #4
        Re: Are C++ strings binary strings?

        ruffiano wrote :
        Hi, can someone tell me if a C++ string (std::string) represents a
        binary or an ASCII string?
        If your question is about whether you can put '\0' in std::strings then
        yes, you can.

        Comment

        • Steve Pope

          #5
          Re: Are C++ strings binary strings?

          Victor Bazarov <v.Abazarov@com Acast.netwrote:
          >ruffiano wrote:
          >Hi, can someone tell me if a C++ string (std::string) represents a
          >binary or an ASCII string?
          >What's "a binary string"?
          >It stores an array of 'char' values. That's all. 'ASCII' is a way
          >to interpret a 'char' value as a printable character. Those two
          >things are orthogonal. If you want to see an 'std::string' as
          >a container of ASCII characters, power to you. On a different system
          >somebody else might see it as a container of EBCDIC characters. Or
          >a container of extended ASCII.
          I think the question is whether a string element is guaranteed
          to represent all 2^k binary values of a k-bit character, and
          exhibit normal binary arithmetic, or whether it's guarateed only to
          represent those values from a character set.

          Some very, very old computers would have character-set-specific
          logic functions (compare, increment, etc.) and one could envision
          a character type, hence a string type on such a machine not
          behaving correctly if used instead as a binary number.

          The odds of running into this are extremely low. What does
          the language say?

          Steve

          Comment

          • Victor Bazarov

            #6
            Re: Are C++ strings binary strings?

            Steve Pope wrote:
            Victor Bazarov <v.Abazarov@com Acast.netwrote:
            >
            >ruffiano wrote:
            >
            >>Hi, can someone tell me if a C++ string (std::string) represents a
            >>binary or an ASCII string?
            >
            >What's "a binary string"?
            >
            >It stores an array of 'char' values. That's all. 'ASCII' is a way
            >to interpret a 'char' value as a printable character. Those two
            >things are orthogonal. If you want to see an 'std::string' as
            >a container of ASCII characters, power to you. On a different system
            >somebody else might see it as a container of EBCDIC characters. Or
            >a container of extended ASCII.
            >
            I think the question is whether a string element is guaranteed
            to represent all 2^k binary values of a k-bit character, and
            exhibit normal binary arithmetic, or whether it's guarateed only to
            represent those values from a character set.
            >
            Some very, very old computers would have character-set-specific
            logic functions (compare, increment, etc.) and one could envision
            a character type, hence a string type on such a machine not
            behaving correctly if used instead as a binary number.
            >
            The odds of running into this are extremely low. What does
            the language say?
            You lost me. According to the language, 'std::string' is a typedef
            of 'std::basic_str ing<char>'. The template 'basic_string' has some
            requirements specified for it in the Standard, but none of them
            mention "binary" or "ASCII". What exactly is it the OP wanted to
            know? And perhaps let the OP answer this.

            V
            --
            Please remove capital 'A's when replying by e-mail
            I do not respond to top-posted replies, please don't ask


            Comment

            • Jim Langston

              #7
              Re: Are C++ strings binary strings?

              "ruffiano" <rough_ruffiano @yahoo.comwrote in message
              news:1162395591 .547327.140410@ b28g2000cwb.goo glegroups.com.. .
              Hi, can someone tell me if a C++ string (std::string) represents a
              binary or an ASCII string?
              >
              Thanks in advance.
              std::string holds chars. Anything you can put into a char (0-255) can be put
              into a std::string, so in this reference, it is binary.


              Comment

              • Michal Nazarewicz

                #8
                Re: Are C++ strings binary strings?

                "ruffiano" <rough_ruffiano @yahoo.comwrote :
                >Hi, can someone tell me if a C++ string (std::string) represents a
                >binary or an ASCII string?
                "Jim Langston" <tazmaster@rock etmail.comwrite s:
                Anything you can put into a char (0-255)
                The range you've given is wrong or at least misleading. I haven't had
                much experience with various platforms but I've never seen a compiler
                which treated char as unsigned by default, and with unsigned chars the
                range would be rather -128..127. Still however, on platforms with
                ones' complement the range would be -127..127. Still however, char
                does not need to have 8 bits (it is guaranteed to have at least 8 bits
                though) so the range could be completely different.

                --
                Best regards, _ _
                .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
                ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
                ooo +--<mina86*tlen.pl >---<jid:mina86*chr ome.pl>--ooO--(_)--Ooo--

                Comment

                • Ron Natalie

                  #9
                  Re: Are C++ strings binary strings?

                  ruffiano wrote:
                  Hi, can someone tell me if a C++ string (std::string) represents a
                  binary or an ASCII string?
                  >
                  Thanks in advance.
                  >
                  It's not necessarily either. It's std::string holds an arbitrary
                  number of whatever "char" is on your machine.

                  Comment

                  • BobR

                    #10
                    Re: Are C++ strings binary strings?


                    Michal Nazarewicz wrote in message <87irhwdebv.fsf @erwin.mina86.c om>...
                    >"ruffiano" <rough_ruffiano @yahoo.comwrote :
                    >>Hi, can someone tell me if a C++ string (std::string) represents a
                    >>binary or an ASCII string?
                    >
                    >"Jim Langston" <tazmaster@rock etmail.comwrite s:
                    >Anything you can put into a char (0-255)
                    >
                    >The range you've given is wrong or at least misleading. I haven't had
                    >much experience with various platforms but I've never seen a compiler
                    >which treated char as unsigned by default,
                    **and with unsigned chars the range would be rather -128..127.**
                    [ Let's not mislead newbies <G]
                    I think Michal meant 'signed char', not 'unsigned char'.

                    #include <limits// and <iostream>, <ostream>
                    {
                    using std::cout // for NG posting
                    cout<<" sizeof(char) ="<<sizeof(char )<<std::endl;
                    cout<<" sizeof(unsigned char) ="<<sizeof(unsi gned char)<<std::end l;

                    cout <<"std::numeric _limits<char>:: max() ="
                    <<int(std::nume ric_limits<char >::max())<<std: :endl;
                    cout <<"std::numeric _limits<char>:: min() ="
                    <<int(std::nume ric_limits<char >::min())<<std: :endl;

                    cout <<"std::numeric _limits<unsigne d char>::max() ="
                    <<int(std::nume ric_limits<unsi gned char>::max())<< std::endl;
                    cout <<"std::numeric _limits<unsigne d char>::min() ="
                    <<int(std::nume ric_limits<unsi gned char>::min())<< std::endl;
                    }

                    /* -- output -- (MinGW(GCC), x86)
                    sizeof(char) =1
                    sizeof(unsigned char) =1
                    std::numeric_li mits<char>::max () =127
                    std::numeric_li mits<char>::min () =-128
                    std::numeric_li mits<unsigned char>::max() =255
                    std::numeric_li mits<unsigned char>::min() =0
                    */

                    --
                    Bob R
                    POVrookie


                    Comment

                    Working...