将数组内的十六进制值显示为stdout,并将输入与这些十六进制值进行比较

时间:2013-10-09 21:45:12

标签: c arrays memcmp

这是我目前的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int target;
    char array[] = {'AC', 'EE', '88', '0D', '87'};

    printf("Enter the target byte as a hex value: \n");
    scanf("%x", &target);

    int i = 0;
    for (i; i < 5; i++)
    {
        printf("Value of array[%d] is %x \n", i, array[i]);
    }

    int i = 0;
    for (i; i < 5; i++)
    {
        if (memcmp(target, array, sizeof(target) == 0))
        {
            printf("Found a match!");
        }
    }
}

(1)我希望能够在stdout中显示char数组[]内的确切值。我尝试过的东西不起作用:将数组显示为%c, %x, %d (2)在我从用户那里得到输入之后,我希望能够将该输入与数组内部的字符进行比较 - 想象一下memcmp是可行的方法,但是如何遍历整个数组呢?我尝试了if (memcmp(target, array[i], sizeof(target) == 0)),但由于array[i]部分我得到运行时错误,但如果我不添加该部分,它将如何通过整个数组比较每个存储的值数组内存位置到target变量中存储的值?我基本上想要将每个数组位置内的字节与用户输入的字节进行比较。

2 个答案:

答案 0 :(得分:1)

  1. 您应该将数组中的十六进制值存储为0xAC等,而不是'AC'(这是一个多字符常量,而不是您想要的任何内容)。
  2. 将您的十六进制值存储为unsigned char,因为在char默认为signed的平台上,您将获得某些值(负值)转换为int并且相应地填充符号位,然后显示。
  3. 您的memcmp通话错误,无论如何都不需要使用它。
  4. 您的示例代码将无法编译,因为您在同一范围内有两个int i声明。如果您不使用C89,为什么不将{for循环编写为for(int i = 0;...)
  5. 不要忘记main的返回值。
  6. 示例:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      int target;
      const unsigned char array[] = { 0xAC, 0xEE, 0x88, 0x0D, 0x87 };
      const int len = sizeof array;
    
      printf("Enter the target byte as a hex value: \n");
      scanf("%x", &target);
    
      for (int i = 0; i < 5; ++i)
      {
        printf("Value of array[%d] is 0x%x\n", i, array[i]);
      }
    
      for (int i = 0; i < len; ++i)
      {
        if (target == array[i])
        {
          printf("Found a match!\n");
        }
      }
    
      return EXIT_SUCCESS;
    }
    

答案 1 :(得分:1)

这个问题是一个更大的计划的一部分,但只是这一小部分,我无法想到开始。最后,我得到了一切。下面是实际程序的最终代码,底部是我在几个用户的帮助下编写的函数定义。再次谢谢你!

#include    <stdio.h>
#include    <stdint.h>
#include    <stdlib.h>


// defined constants
#define MAX_WIDTH       20
#define NUMELEMS        50


// typedefs and function prototypes
typedef unsigned char   Byte;
void DispBytes(void *voidArray, int totalBytes);
int CountMatchBytes(void *voidArray, int totalBytes, int targetHex);


// ==== main ==================================================================
//
// ============================================================================

int     main(void)
{
    auto    double          myDoubles[NUMELEMS];
    auto    int             myInts[NUMELEMS];
    auto    char            myChars[NUMELEMS];
    auto    int             target;
    auto    int             numMatches;

    // process the char array
    puts("Here's the array of chars as bytes: ");
    DispBytes(myChars, sizeof(myChars));
    printf("\nEnter the target byte as a hex value: ");
    scanf("%x", &target);
    numMatches = CountMatchBytes(myChars, sizeof(myChars), target);
    printf("There %s %d matching byte%s.\n\n", (1 == numMatches ? "is" : "are")
                                             , numMatches
                                             , (1 == numMatches ? "" : "s"));
    // process the int array
    puts("\nHere's the array of ints as bytes: ");
    DispBytes(myInts, sizeof(myInts));
    printf("Enter the target byte as a hex value: ");
    scanf("%x", &target);
    numMatches = CountMatchBytes(myInts, sizeof(myInts), target);
    printf("There %s %d matching byte%s.\n\n", (1 == numMatches ? "is" : "are")
                                             , numMatches
                                             , (1 == numMatches ? "" : "s"));
    // process the double array
    puts("\nHere's the array of doubles as bytes: ");
    DispBytes(myDoubles, sizeof(myDoubles));
    printf("Enter the target byte as a hex value: ");
    scanf("%x", &target);
    numMatches = CountMatchBytes(myDoubles, sizeof(myDoubles), target);
    printf("There %s %d matching byte%s.\n\n", (1 == numMatches ? "is" : "are")
                                             , numMatches
                                             , (1 == numMatches ? "" : "s"));

    return  0;

}  // end of "main"

void DispBytes(void *voidArray, int totalBytes)
{
    // Sets startingPtr to the base address of the passed array
    auto unsigned char *startingPtr = voidArray;
    // Sets endingPtr to one address past the array
    auto unsigned char *endPtr = voidArray + totalBytes;
    auto int counter = 0;

    // Loop while the address of startingPtr is less than endPtr
    while (startingPtr < endPtr)
    {
        // Display the values inside the array
        printf("%x ", *startingPtr);
        counter++;

        if (counter == MAX_WIDTH)
        {
            printf("\n");
            counter = 0;
        }
        // Increment the address of startingPtr so it cycles through
        // every value inside the array
        startingPtr++;
    }
}

int CountMatchBytes(void *voidArray, int totalBytes, int targetHex)
{
    // Sets startingPtr to the base address of the passed array
    auto unsigned char *startingPtr = voidArray;
    // Sets endingPtr to one address past the array
    auto unsigned char *endingPtr = voidArray + totalBytes;
    auto int counter = 0;
    // Loop while the address of startingPtr is less than endPtr
    while (startingPtr < endingPtr)
    {
        // If the input value is the same as the value inside
        // of that particular address inside the array,
        // increment counter
        if (targetHex == *startingPtr)
        {
            counter++;
        }
        // Increment the address of startingPtr so it cycles through
        // every value inside the array
        startingPtr++;
    }
    // Return the number of times the input value was found
    // within the array
    return counter;
}