将 size_t 转换为整数 (c++)
我一直在尝试创建一个 for 循环,该循环将根据网络数据包的长度进行迭代。在 API 中,存在一个由 event.packet->dataLength 组成的变量 (size_t)。我想从 0 迭代到 event.packet->dataLength - 7 每次迭代时 i 都会增加 10,但我遇到了麻烦。
我寻找解决方案,但找不到任何有用的东西。我尝试将 size_t 转换为无符号 int 并用它进行算术,但不幸的是它不起作用。基本上我想要的是这样的:
for (int i = 0; i < event.packet->dataLength - 7; i+=10) { }
虽然每次我做这样的事情或尝试进行转换时,我<< # 部分是一个巨大的数字。他们在 API 教程中给出了 printf 语句,该语句使用“%u”来打印实际数字,但是当我将其转换为无符号整数时,它仍然不正确。我不知道从这里该去哪里。任何帮助将不胜感激:)
I've been trying to make a for loop that will iterate based off of the length of a network packet. In the API there exists a variable (size_t) by event.packet->dataLength. I want to iterate from 0 to event.packet->dataLength - 7 increasing i by 10 each time it iterates but I am having a world of trouble.
I looked for solutions but have been unable to find anything useful. I tried converting the size_t to an unsigned int and doing the arithmetic with that but unfortunately it didn't work. Basically all I want is this:
for (int i = 0; i < event.packet->dataLength - 7; i+=10) { }
Though every time I do something like this or attempt at my conversions the i < # part is a huge number. They gave a printf statement in a tutorial for the API which used "%u" to print the actual number however when I convert it to an unsigned int it is still incorrect. I'm not sure where to go from here. Any help would be greatly appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
为什么不改变
i
的类型呢?尽量保持一起使用的所有变量的类型相同;应避免强制转换。
C++03 中没有
size_t
的格式说明符,您必须转换为最大的无符号整数类型并打印它。 (C++0x 中size_t
的格式说明符是%zu
)。但是,无论如何,您都不应该使用 printf:虽然流可能更冗长,但它们类型更安全,并且不需要您记住任何内容。
请记住,您的实际循环逻辑可能有缺陷。 (正如 genpfault 所指出的,当 dataLength - 7 为负数时会发生什么?)
Why don't you change the type of
i
?Try to keep the types of all variables used together the same type; casts should be avoided.
There is no format specifier for
size_t
in C++03, you have to cast to the largest unsigned integer type you can and print that. (The format specifier forsize_t
in C++0x is%zu
). However, you shouldn't be usingprintf
anyway:While streams may be more verbose, they're more type safe and don't require you to memorize anything.
Keep in mind your actual loop logic may be flawed. (What happens, as genpfault notes, when
dataLength - 7
is negative?)用有符号算术来做所有事情。尝试:
一旦您开始使用可能为负值的无符号算术,并使用像
<
这样的比较运算符,您就会遇到麻烦。保持签名要容易得多。Do everything with signed arithmetic. Try:
Once you start using unsigned arithmetic with values that may be negative, and using comparison operators like
<
, you're in trouble. Much easier to keep things signed.数据长度 >= 7 吗?如果dataLength-7的结果为负数,如果将其解释为无符号,则结果是一个非常大的整数。
Is dataLength >= 7? If the result of dataLength-7 is negative, if you interpret it as unsigned, the result is a very large integer.
使用 size_t 作为 i。
对于printf,如果没有C99,只有C90,强制转换为unsigned long,或者unsigned long long。例如:
否则使用 %zu
Use size_t for i.
For printf, if you don't have C99, only C90, cast to unsigned long, or unsigned long long. E.g.:
Otherwise use %zu
您应该首先检查是否
event.packet->dataLength < 7.
。现在,如果它小于 7,您将得到小于 0 的值,用作无符号:例如 0 = 0x00000000; -1 = 0 - 1 = 0xFFFFFFFF。再次,检查:
You should first check if
event.packet->dataLength < 7
. Now if it's less then 7 you get values less than 0 used as unsigned: e.g. 0 = 0x00000000; -1 = 0 - 1 = 0xFFFFFFFF.Again, the check:
“每次我做这样的事情或尝试进行转换时, i < # 部分都是一个巨大的数字。”
这表明原始数据包长度小于 7(您要减去 7)。
一种解决方法是使用实际中足够大的有符号整数类型,标准库为此目的提供了 ptrdiff_t 。例如,
整个内容嵌入到一个
if
中,以检查大小至少为 7。一个更麻烦的解决方法是将 呵呵,
"every time I do something like this or attempt at my conversions the i < # part is a huge number."
That indicates that original packet length is less than 7 (you're subtracting 7).
One fix is to use an in-practice-large-enough signed integer type, and the standard library provides
ptrdiff_t
for that purpose. Like,A more cumbersome workaround is to embed the whole thing in an
if
that checks that the size is at least 7.Cheers & hth.,
由于
event.packet->dataLength
返回无符号类型size_t
:1) 使用
size_t
作为索引变量类型。2) 确保数学不会下溢。 @beldaz。不要从
event.packet->dataLength
中减去 7,而是向i
添加 7。Since
event.packet->dataLength
returns an unsigned typesize_t
:1) Use
size_t
as the index variable type.2) Insure math does not underflow. @beldaz. Rather than subtract 7 from
event.packet->dataLength
, add 7 toi
.