简体   繁体   English

char数组在运行时字符串?

[英]char array to String at runtime?

I have trouble to copy a char array into a String . 我很难将char数组复制到String The following line works fine: 以下行工作正常:

String packetBufferString(packetBuffer);

But obviously I can do this only once, since packetBufferString has to be global and cannot be declared again. 但是显然我只能执行一次,因为packetBufferString必须是全局的,不能再次声明。 The problem is that packetBuffer changes at runtime but I can't update packetBufferString . 问题是packetBuffer在运行时更改,但是我无法更新packetBufferString

The char array is set up with char数组设置为

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];

lg, couka lg,库卡

If String is std::string then you can write 如果Stringstd::string则可以编写

std::string packetBufferString(packetBuffer);

//

packetBufferString.assign(packetBuffer);

or 要么

std::string packetBufferString(packetBuffer);

//

packetBufferString  = packetBuffer;

provided that packetBuffer is zero-terminated. 假设packetBuffer为零终止。 Otherwise you can do the same (that is assigning) but using initial and ending iterators for packetBuffer 否则,您可以执行相同的操作(即分配),但对packetBuffer使用初始和结束迭代器

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

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