如何找到 LPCSTR 的长度

发布于 2024-08-07 03:25:24 字数 177 浏览 4 评论 0原文

我正在尝试使用 atoi() 将 LPCSTR 转换为整数,并验证转换是否成功发生,我想计算它生成的整数和原始 LPCSTR 中的位数(它应该只包含整数)

我我很难找到计算 LPCSTR 长度的好方法。到目前为止,唯一的方法似乎只是计数,直到我到达“/0”

对于更好的方法有什么建议吗?

谢谢

I'm trying to convert an LPCSTR to an integer using atoi(), and to verify that the conversion occurred successfully I want to count the number of digits in the integer it produced and the original LPCSTR (it should contain nothing but integers)

I'm having a hard time finding a good way to calculate the length of the LPCSTR. So far the only way seems to be just counting until I get to a '/0'

Any suggestions for a better method?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

烟沫凡尘 2024-08-14 03:25:24

要查找 LPCTSTR 的长度(在本例中为位数),您应该使用 lstrlen() 而不是 strlen() >。来源:MSDN

To find the length (in this case, the number of digits) of an LPCTSTR, you should be using lstrlen() and not strlen(). Source: MSDN

你げ笑在眉眼 2024-08-14 03:25:24

这不是 strlen 所做的吗?

Isn't that what strlen does?

悟红尘 2024-08-14 03:25:24

您可以使用 strtol 并使用 endptr 返回以检查是否是字符串的结尾(0 字节)。

计数不一定准确。 “00”将得到 0,但 0 有一位数字,而原始字符串的长度为 2。

You could use strtol and use the endptr returned to check if it's the end of the string (0 byte).

Counting will not necessarily be accurate. "00" will result in 0, but 0 has one digit and the original string has length 2.

め可乐爱微笑 2024-08-14 03:25:24

strlen() 函数就是您的正在寻找。

使用示例:

size_t len = strlen( szNumber );

The strlen() function is what you're looking for.

Sample usage:

size_t len = strlen( szNumber );
橘味果▽酱 2024-08-14 03:25:24

没有。

这就是求 c 字符串长度的方法。您可以使用 strlen,但它仍然需要遍历整个字符串并计算 '\0' 之前的字符数。

Nope.

That's how you find the length of a c-string. You could use strlen, but it still has to go down the whole string and count the number of characters before a '\0'.

抠脚大汉 2024-08-14 03:25:24

谈论产生比光更多的热量......:)停止使用“atoi”,这将解决您的大部分问题。 'atoi' 是一个死函数,没有实际价值。将字符串表示形式转换为数字的正确方法是“strto...”组中的函数(“strtol”、“strtoul”等)。这些函数将返回足够的信息给您,以便立即确定是否发生转换错误。

Talk about producing more heat than light... :) Stop using 'atoi' and that will solve most of your problems. 'atoi' is a dead function with no practical value. The proper way to convert a string representation to a number is functions from 'strto...' group ('strtol', 'strtoul' etc.). These functions will return enough information back to you to determine right away whether a conversion error occured or not.

时常饿 2024-08-14 03:25:24
LPCSTR lpText = "test";
long lTextLen = CString(lpText).GetLength();
LPCSTR lpText = "test";
long lTextLen = CString(lpText).GetLength();
帅冕 2024-08-14 03:25:24

我会做一些不同的事情——用输入初始化一个字符串流,读取一个 int,然后检查该流是否为空:

#include <sstream>
#include <iostream>

typedef char const *LPCSTR;

template <class T>
bool check_read(LPCSTR input, T &val) { 
    std::istringstream reader(input);

    reader >> val;
    char ch;
    if (reader >> ch) {
        std::cerr << "\nUnconverted character: " << ch << std::endl;
        return false;
    }
    return true;
}

int main() { 
    LPCSTR inputs[] = {"12345", "54321a"};
    int a;

    for (int i=0; i<2; i++) {
        check_read(inputs[i], a);
        std::cout << "Converted: " << a << std::endl;
    }
    return 0;
}

另一个合理的可能性是 strtol 或其表兄弟之一。它们返回一个指向第一个未转换字符(如果有)的指针,因此它们相当直接地告诉您什么是已转换的,什么是未转换的。它们比流更快,但通常不太灵活——例如,如果您想读取浮点数,上面的 check_read 将按原样工作,但使用 strtol 的内容需要重写。

还有一种可能性,您可以考虑 Boost lexical_cast (它的打包方式略有不同,但与上面的代码非常相似)。

I'd do things a bit differently -- initialize a stringstream with the input, read an int, then check whether the stream is empty:

#include <sstream>
#include <iostream>

typedef char const *LPCSTR;

template <class T>
bool check_read(LPCSTR input, T &val) { 
    std::istringstream reader(input);

    reader >> val;
    char ch;
    if (reader >> ch) {
        std::cerr << "\nUnconverted character: " << ch << std::endl;
        return false;
    }
    return true;
}

int main() { 
    LPCSTR inputs[] = {"12345", "54321a"};
    int a;

    for (int i=0; i<2; i++) {
        check_read(inputs[i], a);
        std::cout << "Converted: " << a << std::endl;
    }
    return 0;
}

Another reasonable possibility would be strtol or one of its cousins. These return a pointer to the first un-converted character (if any), so they tell you fairly directly what was and wasn't converted. They're faster but generally less flexible than streams -- for example, if you want to read a floating point number, the check_read above will work as-is, but something using strtol would need to be rewritten.

As yet one more possibility, you might consider Boost lexical_cast (which is packaged a little differently, but pretty similar to the code above).

愚人国度 2024-08-14 03:25:24

由于LPCSTR只是const char*(去宏之后),strlen就可以了。

但是,如果您需要显式定义与 LPCSTR 输入相对应的 Windows 函数,那么您可以使用 lstrlenA。我严重怀疑调用它和调用 strlen 有任何实际区别。

Since LPCSTR is just const char* (after de-macroing), strlen will be fine.

However if you're after a windows function explicitly defined to correspond with LPCSTR input, then you could use lstrlenA. I seriously doubt there's any practical difference calling this and calling strlen though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文