我有以下代码在我的 ubuntu 系统上运行良好:
#include <算法>
//... 其他函数
bool IsHexPrefixed(const std::string& input) {
返回 input.substr(0, 2) == "0x";
}
std::string StripHexPrefix(const std::string& input) {
返回 IsHexPrefixed(input) 吗? input.substr(2, input.length()) :输入;
}
bool IsHexString(const std::string& input) {
std::string stripped_string_ = StripHexPrefix(input);
返回 std::all_of(stripped_string_.begin(), stripped_string_.end(),::isxdigit);
}
//... 其他一些函数
在 Windows 10 上通过 cmd
, VSCode
, 和 Visual Studio 2019
我弹出窗口提到 Windows 和 Visual Studio 2019 上的调试断言错误。
出现此错误的行是 std::all_of()
函数调用 IsHexString()
功能。
我尝试使用异常并找出异常的来源,但还没有找到解决方案。我也尝试使用 Breakpoint 但这也无助于找到原因。
此错误的原因可能是什么?
编辑:
我传递给 IsHexString()
的字符串功能是 000002C479F17CC0
.
最佳答案
原因正是断言所说的。 isxdigit
如果参数未表示为 unsigned char
,则未定义或 EOF
(见注释 here)。
因为它需要一个 int
参数,很可能您的字符串包含范围内的字符 129-255
(可能通过包含非 ASCII 文本),因此它们被提升为负整数。
链接的 cppreference 页面还有一个解决方法,可以避免您可以应用于您的案例的促销问题:
std::all_of(stripped_string_.begin(), stripped_string_.end(),
[](unsigned char c){ return std::isxdigit(c); });
另一种可能是StripHexPrefix
函数损坏了导致上述问题的字符串。
关于c++ - 获取错误调试断言失败 : Expression c >= -1 && c <= 255,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62592717/