如何比较“4A4B4C” (字符串显示十六进制值)实际字符串是“JKL”

发布于 2024-11-13 22:49:08 字数 185 浏览 3 评论 0原文

我得到一个字符串 "4A4B4C4D4E4F".. 它等于一个字符串 ("JKLMNO")

我如何在 C++ 中检查这种类型的字符串..(一个字符串显示另一个)十六

进制值的十六进制表示

J=4A, K=4B, L=4C

I am getting a string "4A4B4C4D4E4F".. it is equal to a string ("JKLMNO")

How can I check this type of string in c++.. (one string shows the hexadecimal representation of another)

hexadecimal value of

J=4A, K=4B, L=4C

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

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

发布评论

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

评论(3

绅刃 2024-11-20 22:49:08

或者,可以使用 C++ 字符串和功能而不是 C 样式字符串来完成此操作:

#include <iostream>
#include <sstream>
#include <iterator>
#include <algorithm>
#include <iomanip>
#include <string>

bool compareAscii(const std::string& string, const std::string& asciistr) {
   std::ostringstream result;
   result << std::setw(2) << std::setfill('0') << std::hex << std::uppercase;
   // We copy the contents of string, using the range from the beginning to the end.
   // By copying it to an ostream iterator it gets written to the ostringstream,
   // but crucially we treat it as an unsigned int when we write it, which causes
   // the chars to get printed as their numerical values instead.
   std::copy(string.begin(), string.end(), std::ostream_iterator<unsigned int>(result));
   return result.str() == asciistr;
}

namespace {
   const std::string test="JKLMNO";
   const std::string value="4A4B4C4D4E4F";
}

int main() {
   std::cout << "compareAscii(\"" << test << "\", \"" << value << "\") returned " << compareAscii(test, value) << std::endl;
   return 0;
}

Alternatively this can be done using C++ strings and features rather than C-style strings:

#include <iostream>
#include <sstream>
#include <iterator>
#include <algorithm>
#include <iomanip>
#include <string>

bool compareAscii(const std::string& string, const std::string& asciistr) {
   std::ostringstream result;
   result << std::setw(2) << std::setfill('0') << std::hex << std::uppercase;
   // We copy the contents of string, using the range from the beginning to the end.
   // By copying it to an ostream iterator it gets written to the ostringstream,
   // but crucially we treat it as an unsigned int when we write it, which causes
   // the chars to get printed as their numerical values instead.
   std::copy(string.begin(), string.end(), std::ostream_iterator<unsigned int>(result));
   return result.str() == asciistr;
}

namespace {
   const std::string test="JKLMNO";
   const std::string value="4A4B4C4D4E4F";
}

int main() {
   std::cout << "compareAscii(\"" << test << "\", \"" << value << "\") returned " << compareAscii(test, value) << std::endl;
   return 0;
}
也只是曾经 2024-11-20 22:49:08
int hexcmp(const char *string, const char *hex_string)
{
    int len = strlen(string);
    char *hexed_string = new char[2*len+1];
    hexed_string[2*len] = '\0'; //  null-terminate
    for (int i = 0; i < len; i++)
    {
        sprintf(hexed_string, "%X", string[i]);
        hexed_string += 2;
    }
    hexed_string -= 2*len;
    printf("%s\n", hexed_string);
    int res = strcmp(hexed_string, hex_string);
    delete hexed_string;
    return res;
}

if(!hexcmp("JKLMNO", "4A4B4C4D4E4F"))
    printf("Equal\n");
else
    printf("Nonequal\n");
int hexcmp(const char *string, const char *hex_string)
{
    int len = strlen(string);
    char *hexed_string = new char[2*len+1];
    hexed_string[2*len] = '\0'; //  null-terminate
    for (int i = 0; i < len; i++)
    {
        sprintf(hexed_string, "%X", string[i]);
        hexed_string += 2;
    }
    hexed_string -= 2*len;
    printf("%s\n", hexed_string);
    int res = strcmp(hexed_string, hex_string);
    delete hexed_string;
    return res;
}

if(!hexcmp("JKLMNO", "4A4B4C4D4E4F"))
    printf("Equal\n");
else
    printf("Nonequal\n");
吃不饱 2024-11-20 22:49:08

一种方法是转换十六进制字符串中的每两个十六进制数字,并将其转换为十进制值,然后将转换后的数字与要比较的字符串的 ASCII 值进行比较。

显示了示例代码:

int main (void)
{
  char str1[]="4A4B4C4D4E4F";
  char str2[]="JKLMNO";
  char buffer[3];
  int n, i = 0, j = 0, value, flag = 1;

  n = strlen (str1);
  while (i<n)
  {
    buffer[0] = str1[i++];
    buffer[1] = str1[i++];
    buffer[2] = '\0';
    value = strtol (buffer, NULL, 16);
    if (str2[j] != value)
    {
      flag = 0;
      break;
    }
    j++;
  }

  if (flag)
    printf ("\nMatch");
  else
    printf ("\nNo Match");

  printf ("\n");
  reutrn 0;
}

One way is to convert each two hex digits from your hex string and convert it into a decimal value and then compare this converted numeric with the ASCII value of the string you have to be compared.

A sample code is shown:

int main (void)
{
  char str1[]="4A4B4C4D4E4F";
  char str2[]="JKLMNO";
  char buffer[3];
  int n, i = 0, j = 0, value, flag = 1;

  n = strlen (str1);
  while (i<n)
  {
    buffer[0] = str1[i++];
    buffer[1] = str1[i++];
    buffer[2] = '\0';
    value = strtol (buffer, NULL, 16);
    if (str2[j] != value)
    {
      flag = 0;
      break;
    }
    j++;
  }

  if (flag)
    printf ("\nMatch");
  else
    printf ("\nNo Match");

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