如何查找给定的字符串是否符合十六进制表示法,例如。 0x34FF 没有正则表达式?

发布于 2024-12-27 15:18:56 字数 50 浏览 2 评论 0原文

在正则表达式中它将是 0x[0-9a-fA-F]+ ,但是如何在纯 C++ 中实现它?

In regex it would be 0x[0-9a-fA-F]+, but how to achieve it in pure c++ ?

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

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

发布评论

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

评论(6

梦年海沫深 2025-01-03 15:18:57

您可以使用它来代替 strspn 来检查有效的基础。
http://www.cplusplus.com/reference/cstring/strspn/

<强>下面的 C++ 示例此方法检查来自的任何基数有效基数
二进制转base32

/* strspn valid-number example */

#include<iostream>
#include<cstdlib>
#include<cstring>

#define BINARY_BASE 2 /*Defining binary base*/
#define OCTAL_BASE 8  /*Defining octal base*/
#define DECIMAL_BASE 10 /*Defining decimal base*/
#define HEXA_BASE 16    /*Defining hexa-decimal base*/
#define BASE32_BASE 32 /*Defining base32 base*/

bool isValidNumber4Base(const char* numStr,int base)
{

    const char *validBinary = "01";
    const char *validOctal = "01234567";
    const char *validDecimal = "0123456789";
    const char *validHex = "0123456789abcdefxABCDEFX";
    const char *validBase32 = "0123456789abcdefghijklmnopqrstuvABCDEFGHIJKLMNOPQRSTUV";
    const char *validNumber = NULL;

    validNumber = (base == BINARY_BASE) ? validBinary : ((base == OCTAL_BASE) ? validOctal :
                    (base == DECIMAL_BASE) ? validDecimal : (base == HEXA_BASE) ? validHex : (base == BASE32_BASE) ? validBase32 : NULL);

    if(validNumber ==  NULL)
    {
        std::cerr<<"Invalid base encountered"<<std::endl;
        exit(EXIT_FAILURE);
    }

    return (!numStr[strspn(numStr,validNumber)]) ? true : false;
}

/*Test Method */
int main ()
{
  char *hexa_str = "0xFF";  
  std::cout<<"Valid Hexa : "<<std::boolalpha<<isValidNumber4Base(hexa_str,HEXA_BASE)<<std::endl;        
  return 0;
}

You could use this instead strspn to check for valid base.
http://www.cplusplus.com/reference/cstring/strspn/

example below in C++ this method checks for any radix valid base from
binary-to-base32

/* strspn valid-number example */

#include<iostream>
#include<cstdlib>
#include<cstring>

#define BINARY_BASE 2 /*Defining binary base*/
#define OCTAL_BASE 8  /*Defining octal base*/
#define DECIMAL_BASE 10 /*Defining decimal base*/
#define HEXA_BASE 16    /*Defining hexa-decimal base*/
#define BASE32_BASE 32 /*Defining base32 base*/

bool isValidNumber4Base(const char* numStr,int base)
{

    const char *validBinary = "01";
    const char *validOctal = "01234567";
    const char *validDecimal = "0123456789";
    const char *validHex = "0123456789abcdefxABCDEFX";
    const char *validBase32 = "0123456789abcdefghijklmnopqrstuvABCDEFGHIJKLMNOPQRSTUV";
    const char *validNumber = NULL;

    validNumber = (base == BINARY_BASE) ? validBinary : ((base == OCTAL_BASE) ? validOctal :
                    (base == DECIMAL_BASE) ? validDecimal : (base == HEXA_BASE) ? validHex : (base == BASE32_BASE) ? validBase32 : NULL);

    if(validNumber ==  NULL)
    {
        std::cerr<<"Invalid base encountered"<<std::endl;
        exit(EXIT_FAILURE);
    }

    return (!numStr[strspn(numStr,validNumber)]) ? true : false;
}

/*Test Method */
int main ()
{
  char *hexa_str = "0xFF";  
  std::cout<<"Valid Hexa : "<<std::boolalpha<<isValidNumber4Base(hexa_str,HEXA_BASE)<<std::endl;        
  return 0;
}
恋你朝朝暮暮 2025-01-03 15:18:57

您可以使用 std::count_if 来检查计数是否等于大小。

std::string_view v("0xa3");

bool correctPrefix = v.rfind("0x", 0) == 0 && v.length() > 2;
correctPrefix = correctPrefix ? std::count_if(v.begin() + 2,
                         v.end(),
                         ::isxdigit) == v.length() - 2 : false;

You could use std::count_if to check if the count equals the size.

std::string_view v("0xa3");

bool correctPrefix = v.rfind("0x", 0) == 0 && v.length() > 2;
correctPrefix = correctPrefix ? std::count_if(v.begin() + 2,
                         v.end(),
                         ::isxdigit) == v.length() - 2 : false;
醉生梦死 2025-01-03 15:18:56

您可以使用 std::string 的内置方法来检查字符串的第一部分是否为文字“0x”,以及字符串的其余部分是否仅包含允许的字符。这是问题中给出的正则表达式的等效项:

bool is_hex_notation(std::string const& s)
{
  return s.compare(0, 2, "0x") == 0
      && s.size() > 2
      && s.find_first_not_of("0123456789abcdefABCDEF", 2) == std::string::npos;
}

You can use the built-in methods of std::string to check that the first portion of the string is the literal "0x" and that the remainder of the string contains only the allowed characters. Here is the equivalent to the regular expression given in the question:

bool is_hex_notation(std::string const& s)
{
  return s.compare(0, 2, "0x") == 0
      && s.size() > 2
      && s.find_first_not_of("0123456789abcdefABCDEF", 2) == std::string::npos;
}
栩栩如生 2025-01-03 15:18:56

使用 C++11 你可以轻松做到这一点:

std::string str = "FF22ABCD16ZZ";

if (std::all_of(str.begin(), str.end(), ::isxdigit)) {
    std::cout << str << " contains only hexadecimal digits" << std::endl;
}

With C++11 you can do it easily:

std::string str = "FF22ABCD16ZZ";

if (std::all_of(str.begin(), str.end(), ::isxdigit)) {
    std::cout << str << " contains only hexadecimal digits" << std::endl;
}
穿越时光隧道 2025-01-03 15:18:56

调用 strtoul 并检查是否有错误。

Call strtoul and check for an error.

み格子的夏天 2025-01-03 15:18:56

尝试以下操作

bool IsHex(const std::string& str) {
  if (str.length < 3 || str[0] != '0') {
    return false;
  }

  if (str[1] != 'x' && str[1] != 'X') {    
    return false;
  }

  for (size_t i = 2; i < str.length; i++) {
    char current = str[i];
    if (current >= '0' && current <= '9') {
      continue;
    }

    if (current >= 'A' && current <= 'F') {
      continue;
    }

    if (current >= 'a' && current <= 'f') {
      continue;
    }

    return false;
  }

  return true;
}

Try the following

bool IsHex(const std::string& str) {
  if (str.length < 3 || str[0] != '0') {
    return false;
  }

  if (str[1] != 'x' && str[1] != 'X') {    
    return false;
  }

  for (size_t i = 2; i < str.length; i++) {
    char current = str[i];
    if (current >= '0' && current <= '9') {
      continue;
    }

    if (current >= 'A' && current <= 'F') {
      continue;
    }

    if (current >= 'a' && current <= 'f') {
      continue;
    }

    return false;
  }

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