Question about strings in c++

Hi,

These days I’m developing in c++ for the first time, after 6 years working only in .NET and Java. I would like you to help me with two doubts I have:

- In .NET I use string, but in c++ I have to choose between char*, std::string, BSTR, _bstr_t, char[], etc.. How do I know which one to use?

- How can I do something like this in c++?

String someText = “I’ll convert this string into bytes”;
byte[] arrayBytes = getBytes(someText);

Thanks in advance!!
I suggest std::string

you can do something like this:
1
2
3
4
5
6
7
8
#include <string>
using namespace std;

int main()
{
     string someText = "I’ll convert this string into bytes"; //This is also a character array!
     char test = someText[3]; // l 
}
IMHO, char*, char[] are legacy of C.

1
2
String someText = “I’ll convert this string into bytes”;
byte[] arrayBytes = getBytes(someText);

can be represented as
1
2
string someText = “I’ll convert this string into bytes”;
char* arrayBytes = someText.c_str();
when is BSTR, _bstr_t, used
Dunno BSTR and _bstr_t, but I think you can develop your own functions named like .NET's.

If you don't think the std::string class is a good replacement, you can find lots of string libraries on the Internet or even create your own from another or some others, like to split wxWidgets' wxString from wxWidgets and create your own library.
What is wrong with std::string, Jessy?
c_str() returns a const char* so tfityo's example should be
1
2
string someText = “I’ll convert this string into bytes”;
const char* arrayBytes = someText.c_str();


If you need a con-const pointer you can do like this
1
2
string someText = “I’ll convert this string into bytes”;
char* arrayBytes = &someText[0];
Topic archived. No new replies allowed.