How to convert String to unsigned char?

need help please...

define uchar unsigned char

define uint unsigned int

uchar writeData[16]= "test" ; //OK

but

String newData; newData="test"; uchar writeData[16]= newData ; //error: array must be initialized with a brace-enclosed initializer

string FileMeasure="Hello FILE!"
int TempNumOne=sizeof(FileMeasure)
char Filename[100];
for (int a=0;a<=TempNumOne;a++)
        {
            Filename[a]=FileMeasure[a];
        }

I think he's specifically referring to using String class than the array

A conversion from String to c-string might be done like below

String newData;
char buffer[20];
newData="test";         
newData.toCharArray(buffer, 20);

Of course, it helps if you explain WHY you think you need to use a String when you KNOW how many (maximum, at least) characters there are going to be in the String.

thank you very much for all, especially for psppb. its solved. :grin:

cipto: thank you very much for all, especially for psppb. its solved. :grin:

I haven't tested the code fragment that psppb suggested, but it doesn't look as if it would compile, [u]or[/u] accurately copy the string once the compilation errors were corrected.

Is there some reason why you don't simply declare your string as an initialised char array?

PeterH: I haven't tested the code fragment that psppb suggested, but it doesn't look as if it would compile, [u]or[/u] accurately copy the string once the compilation errors were corrected.

??? code is okay for me. What do you think is wrong?

void setup(){
  delay(1000);
  Serial.begin(9600);
  String newData;
  char buffer[20];
  newData="test";         
  newData.toCharArray(buffer, 20);
  Serial.println(newData);
  Serial.println(buffer);
}
void loop(){}

Results:

test
test

required | i need convert string to unsigned char array

//----------------start---------------//
String newData;
char buffer[20];
newData=“test”;
newData.toCharArray(buffer, 20);

unsigned char Data=newData;
//----------------end---------------//

result: error: cannot convert ‘String’ to ‘unsigned char’ in initialization

---------------start--------------
String FileMeasure=“nama sucipt0”;
int TempNumOne=sizeof(FileMeasure);
//char Filename[100];
for (int a=0;a<=16;a++)//TempNumOne;a++)
{
Data[a]=FileMeasure[a];
}
---------------end--------------

result ok, this code work 100% XD

cipto: required | i need convert string to unsigned char array //----------------start---------------// String newData; char buffer[20]; newData="test"; newData.toCharArray(buffer, 20);

unsigned char Data=newData; //----------------end---------------//

Why you ask for help and after made you code? declare buffer as unsigned char, instead char. You know already that you CAN'T assign to array a String (and you made also another error, Data without square) Your original code:

String newData;
newData="test";         
uchar writeData[16]= newData ;
String newData;
newData="test";         
unsigned char writeData[16];
newData.toCharArray(writeData, sizeof(writeData));

Using your variable names.

Code posted from @zoomkat is okay. If you not use String object is better. Use vector instead, String is heavy.

cipto:
String FileMeasure=“nama sucipt0”;
int TempNumOne=sizeof(FileMeasure);
for (int a=0;a<=16;a++)
{ Data[a]=FileMeasure[a];
}
result ok, this code work 100% XD

Happy to know that you made with a for the same work or function toCharArray():

  String FileMeasure="nama sucipt0";
  int TempNumOne=sizeof(FileMeasure); // not used !!!
  FileMeasure.toCharArray(Data,16);    // TempNumOne instead 16

(And where is the declaration of Data? It’s a vector of unsigned char ? Are you sure?

nid69ita:

PeterH:
I haven’t tested the code fragment that psppb suggested, but it doesn’t look as if it would compile, or accurately copy the string once the compilation errors were corrected.

??? code is okay for me. What do you think is wrong?

Here is psppb’s code which I commented on:

psppb:

string FileMeasure="Hello FILE!"

int TempNumOne=sizeof(FileMeasure)
char Filename[100];
for (int a=0;a<=TempNumOne;a++)
       {
           Filename[a]=FileMeasure[a];
       }

The first statement uses an undeclared class string.
The first statement is not terminated by a semicolon.
The second statement is not terminated by a semicolon.
The variable TempNumOne (hey, what a meaningful name!) is set to the size of an instance of String rather than the number of characters in the string so an arbitrary number of characters is copied.
The resulting Filename array is not null terminated.
The OP still hasn’t explained why they are messing around with the String class in the first place. The example is obviously contrived to show the problem, which is fine, but in the example the use of String is pointless - is it actually sensible for the real code to use the String class?

PeterH: Here is psppb's code which I commented on:

Sorry @PeterH, I have not read you refer to @psppb (strange name) code. I thought you refer to @zoomkat code.