ThomasWest ThomasWest - 7 months ago 37
C Question

Compare 2 different hexademical

I have a hexadecimal in

unsigned char *hex_1
that contains:

hex_1[0] = 0x5b
hex_1[1] = 0x83
hex_1[2] = 0xb6
hex_1[3] = 0xe9

and I want to compare it with a hex value:

What should I do? Should I create a new character array, split
, then do memcpy()?

EDIT: I actually want them to either tell me whether "THEY ARE THE SAME!" or "THEY ARE NOT THE SAME!".

EDIT 2: I want it to be like
to be compared with 1c, etc...


Your probably want this:

uint32_t val = *(uint32_t*)(hex_1);  // uint32_t is available by #include <stdint.h>

if (val == 0x1ca0aaf9)


On a big-endian architecture, you are done. On Intel and other little endian architectures you need to decide if that byte array is logicialy meant to be interpreted as in network byte order as 0x5b83b6e9 (‭decimal 1535358697‬). Or if it's meant to be in host byte order (0xe9b6835b) (decimal ‭3921052507‬). If the byte array is in network byte order, then you'll need to swap the bytes. That's what the ntohl function does.

uint32_t val = *(uint32_t*)(hex_1);
val = ntohl(val);    // <arpa/inet.h> or <winsock2.h>

if (val == 0x1ca0aaf9)