简体   繁体   English

将字符串转换为字节数组混淆

[英]Convert string into byte array confusion

How would I properly convert string into byte array?如何正确将字符串转换为字节数组? Let's say my buffer holds: \x1c\x1a\x00\ as a string:假设我的buffer包含: \x1c\x1a\x00\作为字符串:

std::string newbuffer = buffer; // \x1c\x1a\x00
char test[newbuffer.size()] = newbuffer;

I get 'initializing': cannot convert from 'std::string' to 'char .我得到'initializing': cannot convert from 'std::string' to 'char I tried to follow this here, but I get the same error: https://stackoverflow.com/a/505141/8965047我试图在这里遵循这个,但我得到了同样的错误: https://stackoverflow.com/a/505141/8965047

std::string newbuffer = buffer; // \x1c\x1a\x00
char* test = new char(newbuffer.size()+1);
memcpy(test, newbuffer.c_str(), newbuffer.size());

remember delete char*记得删除字符*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM