Are C++ strings binary strings?

R

ruffiano

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

Thanks in advance.
 
B

benben

ruffiano said:
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
 
V

Victor Bazarov

ruffiano said:
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
 
L

loufoque

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.
 
S

Steve Pope

Victor Bazarov said:
ruffiano wrote:
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
 
V

Victor Bazarov

Steve said:
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
 
J

Jim Langston

ruffiano said:
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.
 
M

Michal Nazarewicz

Jim Langston said:
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.
 
R

Ron Natalie

ruffiano said:
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.
 
B

BobR

Michal Nazarewicz wrote in message said:
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
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top