0

I have a long string "given_string", and I'm trying to find hex values within that string. Here's a simplified example:

/* Find a sub-string in a given string: */
    char * given_string = "This is a long given string. 0xff. Happy Programming! ";
    
    int length = strlen(given_string);
    for (int i = 0; i < length; i++)
    {
        if(given_string[i] == 0xff)
        {
            printf("%c\n", given_string[i]);
        }
    }

Though when I run the above code, I get this error:

warning: comparison of constant 255 with expression of type 'char' is always false
      [-Wtautological-constant-out-of-range-compare]
        if(given_string[i] == 0xff)

           ~~~~~~~~~~~~~~~ ^  ~~~~
1 warning generated.

How do I find hex values in a string?

1 Answer 1

0

Perhaps you've overlooked something. All the characters in the string are actually stored as ASCII values. Since ASCII values run from 0 to 127, the code will never find 255.

Maybe you should be looking for the ASCII values representing the hex values you are seeking?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .