简体   繁体   English

比较两个C ++十六进制字符串

[英]Comparing two C++ hex strings

If I have two std::strings which contain a hex value of arbitrary length, how can I compare to the two to see which one is larger in terms of value? 如果我有两个std :: strings,它们包含一个任意长度的十六进制值,我该如何与两者进行比较以查看哪个值更大? Ideally I would like to avoid having to use a big number library. 理想情况下,我希望避免使用大数字库。

I would like to do something like: 我想做类似的事情:

if(hex1 > hex2)
{
    //Do something
}

You can do string comparison on these numbers but there are several potential problems which would have to be accounted for: 您可以对这些数字进行字符串比较,但是有几个潜在的问题需要解决:

  1. A sign character ("+0X2A" will be ordered before "0X0D") 符号字符(“ + 0X2A”将在“ 0X0D”之前订购)
  2. The existence of a "0X" ("0X2A" will be ordered before "0D") 存在“ 0X”(将在“ 0D”之前订购“ 0X2A”)
  3. The capitalization of the "0X" ("0X2A" will be ordered before "0x0D") 大写的“ 0X”(将在“ 0x0D”之前订购“ 0X2A”)
  4. The capitalization of hex digits ("0X0D" will be ordered before "0X0a") 十六进制数字的大写(“ 0X0D”将在“ 0X0a”之前订购)
  5. The zero padding of numbers ("0X002A" will be ordered before "0X0D") 数字的零填充(将在“ 0X0D”之前订购“ 0X002A”)

Consideration for all these conditions and their potential combinations will be a real headache. 考虑所有这些条件及其潜在的组合将是一个真正的头痛。 C++11 introduced the type unsigned long long int which is at least a 64-bit unsigned integer. C ++ 11引入了unsigned long long int类型,该类型至少是一个64位无符号整数。 That gives you a whopping 16 hex characters worth of input, if your input is longer than that this won't work and you will have to parse the string. 如果您输入的内容长于16个十六进制字符,那么这将给您带来多达16个十六进制字符的输入,这将无法正常工作,因此您必须解析字符串。

if(stoll(hex1, 0, 16) > stoll(hex2, 0, 16)

Start by trimming leading zeroes. 首先,修剪前导零。

If they are not equal length, then the longer one is greater. 如果它们的长度不相等,则长度越大越长。

If they are equal length, then loop through them from the beginning and compare each digit. 如果它们的长度相等,则从头开始遍历它们并比较每个数字。 Whichever string first has a larger digit in the same location as the other is greater. 哪一个字符串在相同位置的第一个数字较大,而另一个在更大位置。 If you get to the end and all digits are equal, then the values are equal. 如果到最后并且所有数字都相等,则值相等。

If you are using std::string . 如果您使用的是std::string Works for ASCII encoding. 适用于ASCII编码。

bool hex_greater(std::string &first, std::string &second)
{
    /* Comprasions based on size */
    int firstSize = first.size(); 
    int secondSize = second.size();
    if(firstSize > secondSize)
        return true;
    else if(firstSize < secondSize)
        return false;

    /* Convert to lower case, for case insentitive comprasion */
    std::transform(first.begin(), first.end(), first.begin(), ::tolower);
    std::transform(second.begin(), second.end(), second.begin(), ::tolower);

    /* Call the std::string operator>(...) which compare strings lexicographically */
    if(first > second)
        return true;

    /* In other cases first hex string is not greater */
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM