将std :: ostream转换为某个数组? [英] convert std::ostream to some array?

查看:140
本文介绍了将std :: ostream转换为某个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧式库,该库从硬件获取数据并将其写入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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆