将 size_t 转换为整数 (c++)

发布于 2024-10-11 14:56:04 字数 483 浏览 2 评论 0原文

我一直在尝试创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

楠木可依 2024-10-18 14:56:04

为什么不改变i的类型呢?

for (size_t i = 0; i < event.packet->dataLength - 7; i+=10) { }

尽量保持一起使用的所有变量的类型相同;应避免强制转换。

C++03 中没有 size_t 的格式说明符,您必须转换为最大的无符号整数类型并打印它。 (C++0x 中 size_t 的格式说明符是 %zu)。但是,无论如何,您都不应该使用 printf:

std::cout << i; // print i, even if it's a size_t

虽然流可能更冗长,但它们类型更安全,并且不需要您记住任何内容。

请记住,您的实际循环逻辑可能有缺陷。 (正如 genpfault 所指出的,当 dataLength - 7 为负数时会发生什么?)

Why don't you change the type of i?

for (size_t i = 0; i < event.packet->dataLength - 7; i+=10) { }

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 for size_t in C++0x is %zu). However, you shouldn't be using printf anyway:

std::cout << i; // print i, even if it's a size_t

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?)

蓝梦月影 2024-10-18 14:56:04

用有符号算术来做所有事情。尝试:

for (int i = 0; i < int(event.packet->dataLength) - 7; i+=10) { }

一旦您开始使用可能为负值的无符号算术,并使用像 < 这样的比较运算符,您就会遇到麻烦。保持签名要容易得多。

Do everything with signed arithmetic. Try:

for (int i = 0; i < int(event.packet->dataLength) - 7; i+=10) { }

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.

メ斷腸人バ 2024-10-18 14:56:04

数据长度 >= 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.

苄①跕圉湢 2024-10-18 14:56:04

使用 size_t 作为 i。

对于printf,如果没有C99,只有C90,强制转换为unsigned long,或者unsigned long long。例如:

for (size_t i = 0; i < 10; ++i)
        //printf("%llu\n", (unsigned long long)i);
        printf("%lu\n", (unsigned long)i);

否则使用 %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.:

for (size_t i = 0; i < 10; ++i)
        //printf("%llu\n", (unsigned long long)i);
        printf("%lu\n", (unsigned long)i);

Otherwise use %zu

神也荒唐 2024-10-18 14:56:04

您应该首先检查是否 event.packet->dataLength < 7.。现在,如果它小于 7,您将得到小于 0 的值,用作无符号:例如 0 = 0x00000000; -1 = 0 - 1 = 0xFFFFFFFF。

再次,检查:

if (event.packet->dataLength < 7) {
  ...
} else {
  for (size_t i = 0; i < event.packet->dataLength - 7; i+=10) { }
}

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:

if (event.packet->dataLength < 7) {
  ...
} else {
  for (size_t i = 0; i < event.packet->dataLength - 7; i+=10) { }
}
半世蒼涼 2024-10-18 14:56:04

“每次我做这样的事情或尝试进行转换时, i < # 部分都是一个巨大的数字。”

这表明原始数据包长度小于 7(您要减去 7)。

一种解决方法是使用实​​际中足够大的有符号整数类型,标准库为此目的提供了 ptrdiff_t 。例如,

#include <stdlib.h>   // Not sure, but I think it was this one.

typedef ptrdiff_t    Size;
typedef Size         Index;

void foo()
{
    // ...
    for( Index i = 0; i < Size( event.packet->dataLength ) - 7; i += 10 )
    {
        // ...
    }
}

整个内容嵌入到一个 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,

#include <stdlib.h>   // Not sure, but I think it was this one.

typedef ptrdiff_t    Size;
typedef Size         Index;

void foo()
{
    // ...
    for( Index i = 0; i < Size( event.packet->dataLength ) - 7; i += 10 )
    {
        // ...
    }
}

A more cumbersome workaround is to embed the whole thing in an if that checks that the size is at least 7.

Cheers & hth.,

不打扰别人 2024-10-18 14:56:04

由于 event.packet->dataLength 返回无符号类型 size_t

1) 使用 size_t 作为索引变量类型。

2) 确保数学不会下溢。 @beldaz。不要从 event.packet->dataLength 中减去 7,而是向 i 添加 7。

// for (int i = 0; i < event.packet->dataLength - 7; i+=10) { }
for (size_t i = 0; i + 7 < event.packet->dataLength; i += 10) { }

Since event.packet->dataLength returns an unsigned type size_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 to i.

// for (int i = 0; i < event.packet->dataLength - 7; i+=10) { }
for (size_t i = 0; i + 7 < event.packet->dataLength; i += 10) { }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文