C++ std::string与char[]的互转

C++ std::string与char[]的互转

  • 1 char[]转std::string
  • 2 std::string转char[]

1 char[]转std::string

char arry[5] = {0x03,0xaf,0x34,0x65,0x55};
//方法1
std::string strArry(arry);
//方法2
std::string strArry(arry,sizeof(arry));

2 std::string转char[]

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;

你可能感兴趣的:(C++,c++,算法,开发语言)