如何找到 LPCSTR 的长度
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
要查找
LPCTSTR
的长度(在本例中为位数),您应该使用lstrlen()
而不是strlen()
>。来源:MSDNTo find the length (in this case, the number of digits) of an
LPCTSTR
, you should be usinglstrlen()
and notstrlen()
. Source: MSDN这不是 strlen 所做的吗?
Isn't that what strlen does?
您可以使用
strtol
并使用endptr
返回以检查是否是字符串的结尾(0 字节)。计数不一定准确。 “00”将得到 0,但 0 有一位数字,而原始字符串的长度为 2。
You could use
strtol
and use theendptr
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.
strlen() 函数就是您的正在寻找。
使用示例:
The strlen() function is what you're looking for.
Sample usage:
没有。
这就是求 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'
.谈论产生比光更多的热量......:)停止使用“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.
我会做一些不同的事情——用输入初始化一个字符串流,读取一个 int,然后检查该流是否为空:
另一个合理的可能性是 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:
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).
由于
LPCSTR
只是const char*
(去宏之后),strlen
就可以了。但是,如果您需要显式定义与
LPCSTR
输入相对应的 Windows 函数,那么您可以使用lstrlenA
。我严重怀疑调用它和调用strlen
有任何实际区别。Since
LPCSTR
is justconst 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 uselstrlenA
. I seriously doubt there's any practical difference calling this and callingstrlen
though.