How to fill a string with random (hex) characters?
c, linux
Solution
- string is an array of `char`-s not `unsigned char`-s
- you are using `str[i]` (which is of type `unsigned char`) as a 1st argument to `sprintf`, but it requires type `char *` (pointer).
This should be a little better:
char str[STR_LEN + 1];
for(i = 0; i < STR_LEN; i++) {
sprintf(str + i, "%x", rand() % 16);
}
Problem
I have a string (unsigned char) and i want to fill it with only hex characters. my code is ``` unsigned char str[STR_LEN] = {0}; for(i = 0;i<STR_LEN;i++) { sprintf(str[i],"%x",rand()%16); } ``` Of course, when running this I get segfaulted