Windows phone 8 从DataReader高效地读取一些字节?
我有一个带有ANSI字符串的流。它的前缀是字节长度。如何将其读入Windows phone 8 从DataReader高效地读取一些字节?,windows-phone-8,datareader,c++-cx,Windows Phone 8,Datareader,C++ Cx,我有一个带有ANSI字符串的流。它的前缀是字节长度。如何将其读入std::string 比如: short len = reader.readInt16(); char[] result = reader.readBytes(len); // ??? std::string str = std::copy(result, result + len); 但是没有方法readBytes(int) 附带问题:使用readByte()从DataReader一次读取一个字节是否很慢?根据MSDN,Dat
std::string
比如:
short len = reader.readInt16();
char[] result = reader.readBytes(len); // ???
std::string str = std::copy(result, result + len);
但是没有方法readBytes(int)
附带问题:使用
readByte()
从DataReader
一次读取一个字节是否很慢?根据MSDN,DataReader::ReadBytes是存在的,并且是您正在寻找的:
它将Platform::Array
作为参数,您可能会使用前缀长度初始化它,返回时将包含字节。从这里开始,构造所需的std::string
是一个乏味但简单的过程
基本用法如下所示(抱歉,目前在Mac电脑上,精确的语法可能有点错误):
auto len=reader->ReadInt16();
自动数据=参考新平台::阵列(len);
读卡器->读取字节(数据);
//现在数据有了您需要的字节,您可以用它生成字符串
请注意,上面的代码尚未准备好生产—很可能reader
没有足够的缓冲数据,因此您需要reader.LoadAsync(len)
并在数据可用时创建一个continuation来处理数据。尽管如此,希望这足以让你走
编辑:
刚刚注意到你的附带问题。简单的回答是,是的,一次读取一个字节的速度要慢得多,因为这需要更多的工作
长回答:考虑每个字节的内容:
std::string
,偶尔会导致动态重新分配(除非您已经str.resize(len)
)现在,考虑一下一次读取所有字节时发生的情况:
auto len = reader->ReadInt16();
auto data = ref new Platform::Array<uint8>(len);
reader->ReadBytes(data);
// now data has the bytes you need, and you can make a string with it