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

后端 未结 8 1528
闹比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:55

    string uInput;
    cout << "Enter message" << endl;
    getline(cin, uInput);
    

    I'm adding 1 here because in c strings we have '\0' at the end

    unsigned char dataS[uInput.size()+1];
    strcpy(reinterpret_cast(dataS), uInput.c_str());
    

    I think this example will help others more in the future.

提交回复
热议问题