将std :: ostream转换为某个数组? [英] convert std::ostream to some array?
问题描述
我有一个旧式库,该库从硬件获取数据并将其写入ostream
.
该方法如下所示:
I have a legacy library that takes data from hardware and writes it to ostream
.
The method looks like following :
int sensors(ostream*) const;
我对古代方式还不够熟练.如何将此数据转换为QByteArray
?或者,至少,到已知大小的char
数组?
我本来可以解决这个问题,但是还有一个问题:ostream中的数据似乎是任意长度的,并且具有几个任意的'\0'
符号,因此您不能指望它以null结尾.
I am not skilled enough in Ancient Ways. How to convert this data to QByteArray
? Or, at least, to char
array of known size?
I would have solved it myself, but there is an additional problem: the data in ostream seem to be arbitrary length and have several arbitrary '\0'
symbols, so you can't count on it being null-terminated.
推荐答案
我认为这是OrcunC正在获得的:
I think this is what OrcunC was getting at:
std::stringstream s;
sensors( &s );
QByteArray( s.str().data(), (int) s.str().size() );
...但希望更加清楚:).另请参见 std :: stringstream 和
... but hopefully more clear :). See also std::stringstream and std::string for information on the classes/member functions used here. By the way, note that I am using str().data(), not str().c_str() -- I'm being really careful to handle those \0
characters, and I'm not assuming NULL termination.
这篇关于将std :: ostream转换为某个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!