c++ - Convert QString into unsigned char array -


I have a very basic question, I tried googling for a while, because there are so many similar questions, but No solution worked for me.

Here is a code snippet that shows the problem:

  Caststring test = "Hello"; Unsigned four * test1 = (unsigned char *) test.data (); Unsigned four test 2 [10]; Memcpy (test2, test1, test.size ()); Std :: Court & lt; & Lt; Test2;  

I try to fit QString into an unsigned char array, but the option I get is always 'h'.

Can anyone tell me what is going wrong here?

The problem is in that QString.data () QChar * but you want const char *

  QString test = "hello"; Unsigned four test 2 [10]; Memcpy (test2, test.toStdString () .c_str (), test.size ()); Test 2 [5] = 0; QDebug () & lt; & Lt; (Four *) test2; ^^^ This is necessary because otherwise the address has been printed, i.e. 0x7fff8d2d0b20  

assignment

  unsigned char * test1 = (unsigned char *) Test (info);  

And attempt to copy

  Unsigned four trials 2 [10]; Memcpy (test2, test1, test.size ());  

is incorrect, because as and to attempt to copy, it will end only 0 after code 0. .


Comments