Converting string to unsigned char

Hi there,

I’m working on a ofxSimpleSerial-addon and because people like to work with strings I would like to add a writeString() method.
But… I don’t understand how I should convert strings into unsigned char’s, and that is what the original writeBytes needs.

Just one of the things I tried was:

  
string valString = "test";  
unsigned char valChar[10];  
strcpy(valString.c_str(), valChar);  

But then I got the following warning on the line with the strcpy.

Invalid conversion from ‘const char*’ to ‘char*’

Can any of you guys / girls help?
Thanx!

try

  
strcpy((char*)valString.c_str(), valChar);  

maybe a simple cast can resolve your problem:

  
  
	string a = "the book is on the table"; // string a  
	unsigned char* b = (unsigned char*) a.c_str(); // cast from string to unsigned char*  
	ofDrawBitmapString(a, 30, 100); // draws a  
	ofDrawBitmapString(string((char *)b), 30, 130); // draws b   
}  
  

cheers,

  
strcpy((char*)valString.c_str(), valChar);  

Gives me:

Invalid conversion from ‘const char*’ to ‘char*’

But the following code seems to do the trick :slight_smile:

unsigned char* valChar = (unsigned char*) valString.c_str(); // cast from string to unsigned char*

Thnx for the help guys.

Now let’s see if the arduino gets the right messages…