I’m basicly trying to compare a token(string) with a hex-datatype!
Example:
#include<stdio.h>
#include<string.h>
int main(int argc, char *argv[]) {
char* token ="Hello";
int hex = 0x2F;
if(strncmp(token, "Hil", 3)==0){
printf("Token founde"); //works!
}else{
printf("sorry seems to be the hardest word");
}
}
This actually works, of course.
I’m comparing 2 strings with at least n charachters…no problem.
But now I want to do the same thing with a hex-value instead of a string!
Something like this:
int hex = 0x2F;
if(strncmp((char*)hex, "0x", 2)==0){
printf("Token founde"); //works!
}else{
printf("sorry seems to be the hardest word");
}
}
Since strncmp compares strings only, I tried to cast the hex-value to a char*…
It doesn’t work at all.
I’ve searched everywhere… even in my “The programming language C” book…but I can’t figure out how to solve this problem.
In java, I’d use something like startsWith etc… but I don’t know if there is a familiar function in c that could have the same effect.
Hm, I am not sure that this is what you want, but right now you are converting the value of hex to an (most certainly invalid) address. To do what you want to do you have to take the address:
strncmp((char*)&hex, "0x", 2)but I cannot recommend that.I suggest that you transform the hex into a string first and then compare: