Question Details

No question body available.

Tags

c type-conversion

Answers (2)

Accepted Answer Available
Accepted Answer
June 9, 2026 Score: 1 Rep: 221,803 Quality: High Completeness: 60%

What won't work:

Casting to (unsigned) without a type specified means unsigned int, so you are casting the address of the array to an unsigned int, which is plain wrong.

Remember that when we use an array name in an expression, it "decays" to a pointer to the first element in that array. So stringhexadecimal gives us an address, with a pointer type.

So even if you used (unsigned char) that wouldn't work either, because you don't want to cast the address to another type no matter.

And casting to (unsigned char*) doesn't work either, but this time because you can't assign to arrays in C - the language is simply designed in that way. Nor can you initialize an array with a pointer.


Solution:

In C it is safe to "type pun" between any of the character types, so simply use a union. Then you don't allocate the array twice either, saving memory. This is the correct approach when you want to represent the same memory area using various different types.

#include 

typedef union { char c
hex [100]; unsigned char uhex [100]; } hexstrt;

int main() { hexstrt hexstr = { .chex = "5a2be26e5ef51ad833c153" };

// then do something with hexstr.u_hex }
June 9, 2026 Score: 4 Rep: 460 Quality: Medium Completeness: 60%

Your cast is trying to convert the address of the array stringhexadecimal into one unsigned int, not convert the characters inside the string.

Case 1: You want to treat the same string as unsigned char *

you can use a pointer:

#include 

int main() { char stringhexadecimal[100] = "5a2be26e5ef51ad833c153"; unsigned char hexadecimal = (unsigned char)stringhexadecimal; printf("%s\n", (char*)hexadecimal); }

No copy happens here, hexadecimal points to the same memory as stringhexadecimal, just with different type.

Case 2: You want to copy the same string as unsigned char

Use strcpy or memcpy:

#include 
#include 

int main() { char stringhexadecimal[100] = "5a2be26e5ef51ad833c153"; unsigned char hexadecimal[100]; strcpy((char*)hexadecimal, stringhexadecimal); // copies until '\0' (the null terminator), including the '\0', leaves the rest bytes uninitialized // or memcpy(hexadecimal, stringhexadecimal, sizeof(stringhexadecimal)); // copies all 100 bytes of the array // or memcpy(hexadecimal, stringhexadecimal, strlen(stringhexadecimal) + 1); // same effect as strcpy printf("%s\n", (char)hexadecimal); }

Case 3: You want to convert hex text into real bytes

Your string has hex pairs:

5a 2b e2 6e 5e f5 1a d8 33 c1 53

To convert those into unsigned char byte values:

#include 
#include 

int main() { char string_hexadecimal[100] = "5a2be26e5ef51ad833c153"; unsigned char bytes[100]; size_t len = strlen(string_hexadecimal); if (len % 2 != 0) { printf("Invalid hex string length\n"); return 1; } size_t byte_count = len / 2; for (size_t i = 0; i < byte_count; i++) { unsigned int value; sscanf(&string_hexadecimal[i 2], "%2x", &value); bytes[i] = (unsigned char)value; } for (sizet i = 0; i < bytecount; i++) printf("%02x(%d) ", bytes[i], bytes[i]); printf("\n"); }

Output:

5a(90) 2b(43) e2(226) 6e(110) 5e(94) f5(245) 1a(26) d8(216) 33(51) c1(193) 53(83)