Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

Equivalent of Java's String.getBytes() in C++?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

char/unsigned char types are an equivalent of byte in Java or other languages.

std::string has method c_str(), which returns char* with the length of std::string::size(). char*, being a c-string, can be considered as a full and complete binary representation of the underlying string.

Basically,

std::string s;

const char* s_data = s.c_str();
// do what you want with s_data

If all you want is an access to characters, using operator[] is enough:

char p = s[i]; // process that p as you want!

Information, provided here, should be enough for any kind of interaction with strings.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...