How to convert a string literal to unsigned char array in visual c++

后端 未结 8 1529
闹比i
闹比i 2020-12-06 06:02

How to convert a string to Unsigned char in c++...

I have,

unsigned char m_Test[8];

I want to assign a string \"Hello world\"

8条回答
  •  我在风中等你
    2020-12-06 06:38

    Firstly, the array has to be at least big enough to hold the string:

     unsigned char m_Test[20];
    

    then you use strcpy. You need to cast the first parameter to avoid a warning:

     strcpy( (char*) m_Test, "Hello World" );
    

    Or if you want to be a C++ purist:

     strcpy( static_cast ( m_Test ), "Hello World" );
    

    If you want to initialise the string rather than assign it, you could also say:

     unsigned char m_Test[20] = "Hello World";
    

提交回复
热议问题