[英]c++ programming with linux
i am receiving a buffer with float values like -157.571 91.223 -165.118 -59.975 0.953 0 0.474 0 0 0.953 0 0.474 0.474 0 5.361 0 0 0.474 0 5.361...but they are in characters...now i want to retrieve one by one value and put it in a variable...can any one help me please...i have used memcpy but no use..if i am copying 8 bytes its taking as -157.571 with 8 values including '-' and '.' 我收到的浮点值为-157.571 91.223 -165.118 -59.975 0.953 0 0.474 0 0 0.953 0 0.474 0.474 0 5.361 0 0 0.474 0 5.361 ...一个值并将其放在变量中...可以有人帮我...我用过memcpy但没有用..如果我要复制8字节的值-157.571,其中包括'-'和'等8个值。 ' .... is there any solution for this .. ....对此有任何解决方案..
If I'm understanding correctly, you have a value stored in a string of some kind and you want to retrieve a floating point value out of it. 如果我理解正确,则您有一个存储在某种类型的字符串中的值,并且想要从中检索浮点值。 If that is the case, it depends on the language you're using. 如果是这种情况,则取决于您使用的语言。 If you're using C++, you should use a std::istringstream
to perform the conversion. 如果使用的是C ++,则应使用std::istringstream
执行转换。 If you're using C, (and/or the cstdio
system from C++ instead of iostream
), you should use sscanf
. 如果您使用的是C(和/或C ++的cstdio
系统而不是iostream
),则应使用sscanf
。 If you're using C#, you should be using Double.TryParse
. 如果您使用的是C#,则应该使用Double.TryParse
。
Let's say your buffer of floats is this string : 假设您的float缓冲区是以下字符串 :
"-157.571 91.223 -165.118 -59.975 0.953 0 0.474 0 0 0.953 0 0.474 0.474 0 5.361 0 0 0.474 0 5.361"
your cleanest C++ approach is to load this into a std::istringstream
and then use the stream to extract the float values.. ie 您最干净的C ++方法是将其加载到std::istringstream
,然后使用该流提取浮点值。即
std::istringstream str(buffer);
now you can use the stream in operator to extract a float value, and repeat this until there are no more (hint: check the stream flags) 现在您可以使用流中的in运算符提取浮点值,然后重复此操作,直到没有更多为止(提示:检查流标志)
str >> {float}; // then do something with {float}
Optionally you can push this extracted value in to a std::vector
to give you the floats in the string. (可选)您可以将此提取的值推入std::vector
以便为字符串提供浮点数。 I've not written out the full code, just the pseudo to give you an idea... 我没有写完整的代码,只是伪代码给你一个主意...
You've got a string that contains a number of floating point values separated by a space. 您有一个字符串,其中包含多个浮点值,并用空格分隔。
If You could use strtof()
to convert them to float
values one at a time. 如果可以使用strtof()
将它们转换为float
值。
float strtof( const char *nptr, char **endptr);
where
will contain the pointer to the last character that was converted, so can be used to walk through your string.nptr
is the string that you wish to convert and endptr
is a pointer to a char pointer. endptr
eg. 例如。
char *rawString; char **walkPtr; float convertedValue; /* do something to collect the next series of floats */ /* and now do the conversions */ *walkPtr = rawString; while( still_some_string_to_process ) { convertedValue = strtof( *walkPtr, walkPtr ); // increment the pointer to skip over the delimiting space *walkPtr++; }
Appropriate error checking should be applied to ensure you don't run off the end of the string, etc. 应该应用适当的错误检查,以确保您不会用尽字符串的末尾等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.