C++ std::string与char[]的互转
- [1 char[]转std::string](#1 char[]转std::string)
- [2 std::string转char[]](#2 std::string转char[])
1 char[]转std::string
cpp
char arry[5] = {0x03,0xaf,0x34,0x65,0x55};
//方法1
std::string strArry(arry);
//方法2
std::string strArry(arry,sizeof(arry));
2 std::string转char[]
cpp
std::cout << "hex:" << std::hex;
char* bytes = new char[strArry.length()+1];
std::strcpy(bytes, strArry.c_str());
for (int i = 0; i < strArry.length(); i++)
std::cout << (bytes[i] & 0xff);
std::cout << std::dec << std::endl;