convert string into character

for everything else
Post Reply
roarsinc2011
Posts: 3
Joined: Fri Sep 23, 2011 12:08 am

convert string into character

Post by roarsinc2011 »

how to convert string into character format ?
v8dave
Posts: 333
Joined: Thu Dec 31, 2009 8:31 pm

Re: convert string into character

Post 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...
User avatar
pbreed
Posts: 1081
Joined: Thu Apr 24, 2008 3:58 pm

Re: convert string into character

Post 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?
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: convert string into character

Post 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
Post Reply