By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Are C++ strings binary strings?

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

Thanks in advance.

Nov 1 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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
Nov 1 '06 #2

P: n/a
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
Nov 1 '06 #3

P: n/a
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.
Nov 1 '06 #4

P: n/a
Victor Bazarov <v.********@comAcast.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
Nov 1 '06 #5

P: n/a
Steve Pope wrote:
Victor Bazarov <v.********@comAcast.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_string<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
Nov 1 '06 #6

P: n/a
"ruffiano" <ro************@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.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.
Nov 2 '06 #7

P: n/a
"ruffiano" <ro************@yahoo.comwrote:
>Hi, can someone tell me if a C++ string (std::string) represents a
binary or an ASCII string?
"Jim Langston" <ta*******@rocketmail.comwrites:
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*chrome.pl>--ooO--(_)--Ooo--
Nov 3 '06 #8

P: n/a
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.

Nov 3 '06 #9

P: n/a

Michal Nazarewicz wrote in message <87************@erwin.mina86.com>...
>"ruffiano" <ro************@yahoo.comwrote:
>>Hi, can someone tell me if a C++ string (std::string) represents a
binary or an ASCII string?

"Jim Langston" <ta*******@rocketmail.comwrites:
>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(unsigned char)<<std::endl;

cout <<"std::numeric_limits<char>::max() ="
<<int(std::numeric_limits<char>::max())<<std::endl ;
cout <<"std::numeric_limits<char>::min() ="
<<int(std::numeric_limits<char>::min())<<std::endl ;

cout <<"std::numeric_limits<unsigned char>::max() ="
<<int(std::numeric_limits<unsigned char>::max())<<std::endl;
cout <<"std::numeric_limits<unsigned char>::min() ="
<<int(std::numeric_limits<unsigned char>::min())<<std::endl;
}

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

--
Bob R
POVrookie
Nov 3 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.