Page 1 of 1

convert string into character

Posted: Mon Oct 10, 2011 3:53 am
by roarsinc2011
how to convert string into character format ?

Re: convert string into character

Posted: Mon Oct 10, 2011 3:55 am
by v8dave
Hi there,

It would help if you could explain more of what you want to do with the string?

The main reason for doing something like this is sending bytes to a serial port or other device.

An explanation would help us work out what you need.

Cheers.
Dave...

Re: convert string into character

Posted: Mon Oct 10, 2011 7:58 am
by pbreed
Can you be more clear on what you mean by string?

A C++ string class?

A null terminated character array ie
const char * foo="This is a char array";

???

Not really sure what your question is?

Re: convert string into character

Posted: Mon Oct 10, 2011 11:19 am
by tod
The only thing that makes sense as a question to me is how to turn a C++ string into a series of c characters, which is what the c_str() function can help with.

Code: Select all

  using namespace std;
  string a_cpp_string ("some string");
  a_c_char_array= new char [a_cpp_str.size()+1];
  strcpy (a_c_char_array, a_cpp_string.c_str()); //c_str returns null terminated sequence so strcpy is safe