Random int 1-255 for character in C

I have a function that returns an integer from 1 to 255. Is there a way to turn this int into a character, which I can strcmp(), into an existing string.

Basically, I need to create a string of letters (all ASCII values) from PRNG. Everything works for me minus int to char. There is no function Chr()like in PHP.

+5
source share
5 answers

A charin C can take values ​​from CHAR_MINto CHAR_MAX. If charsigned, CHAR_MAXmay be less than 255 (for example, the total value is 127). If charnot specified, CHAR_MAXmust be at least 255.

, char , char (, ). char , . , , mod 128 char.

, ASCII, (ASCII 127).

, : char ASCII, ASCII — EBCDIC.

+8

char ( ). (strcat, strcmp ..), char ('\0') ( )....

int num = myrand(1,255);

char str[2] = {(char)num, '\0'};

...

if(strcmp(str, otherString...
+6

char:

char c = (char)myRandomInt;
+5

A char - 255, , 'a', .

char.

char C = 67;
char c = 'c';
char zero = 'c' - 'c';
char also_zero = c - 'c';
/*Note: zero == 0*/

- strcmp, char*. ( ), char.

, :

char szp[] = {c, '\0'};
int isDifferent = strcmp(szp, "c");
+4

In C, a charis basically intbetween 0 and 255, so you can treat the integer directly as a char if you want to use sprintf for a buffer or similar. You might want your function to return charto make it obvious what is happening.

+1
source

All Articles