随机 int 1-255 到 C 中的字符

发布于 2024-08-23 01:40:11 字数 191 浏览 10 评论 0原文

我有一个返回 1 到 255 之间整数的函数。有没有办法将此 int 转换为字符,我可以将 strcmp() 转换为现有字符串。

基本上,我需要从 PRNG 创建一串字母(所有 ASCII 值)。除了 int 到 char 部分之外,我已经完成了所有工作。没有像 PHP 中那样的 Chr() 函数。

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

Basically, I need to create a string of letters (all ASCII values) from a PRNG. I've got everything working minus the int to char part. There's no Chr() function like in PHP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

骄兵必败 2024-08-30 01:40:12

C 中的 char 只能采用 CHAR_MINCHAR_MAX 之间的值。如果 char 有符号,CHAR_MAX 可能小于 255(例如,常见值为 127)。如果 char 是无符号的,CHAR_MAX 必须至少为 255。

假设您的 char 是无符号的,您可以将随机数分配给 < code>char (例如在您的字符串中)。如果 char 有符号,你必须更加小心。在这种情况下,您可能希望将值 mod 128 分配给您的 char

事实上,由于您正在处理 ASCII,您可能无论如何都想这样做(ASCII 最多只能达到 127)。

最后,强制性的可移植性说明:如果底层编码不是 ASCII,则 char 的整数值可能无法表示其 ASCII 值(例如 EBCDIC)。

A char in C can only take values from CHAR_MIN to CHAR_MAX. If char is signed, CHAR_MAX may be less than 255 (for example, a common value is 127). If char is unsigned, CHAR_MAX has to be at least 255.

Assuming your char is unsigned, you can just assign the random number to a char (in your string for example). If char is signed, you have to be more careful. In this case, you probably want to assign the value mod 128 to your char.

In fact, since you are dealing with ASCII, you may want to do that anyway (ASCII is only up to 127).

Finally, obligatory portability remark: a char's value as an integer may not represent its ASCII value, if the underlying encoding is not ASCII—an example is EBCDIC.

半窗疏影 2024-08-30 01:40:12

只需将其转换为 char(如下所示)。如果要在字符串函数(strcat、strcmp 等)中使用它,则它必须位于末尾带有空终止符 ('\0') 的 char 数组中(也如下所示)....

int num = myrand(1,255);

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

...

if(strcmp(str, otherString...

Just cast it to a char (as shown below). If you're going to use it in a string function (strcat, strcmp, etc), then it has to be in a char array with a null terminator ('\0') at the end (also shown below)....

int num = myrand(1,255);

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

...

if(strcmp(str, otherString...
星軌x 2024-08-30 01:40:12

只需将其转换为字符即可:

char c = (char)myRandomInt;

Just cast it to a char:

char c = (char)myRandomInt;
想念有你 2024-08-30 01:40:12

char 只是一个具有 255 值范围的整数,而像 'a' 这样的字符文字也只是一个数字。

只需转换为字符即可。

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

要使用像 strcmp 这样的东西,你需要一个 char* 。该函数需要一个指向字符数组的第一个元素(以零结尾)的指针,而不是单个字符的地址。

所以你想要:

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

A char is just an integer with a range of 255 values, and character literals like 'a' are just a number as well.

Just cast to a char.

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

To use something like strcmp you need a char* though. The function expects a pointer to the first element of an array of chars (zero terminated) though and not the address of a single char.

So you want:

char szp[] = {c, '\0'};
int isDifferent = strcmp(szp, "c");
演多会厌 2024-08-30 01:40:12

在 C 中,char 基本上是 0 到 255 之间的 int,因此如果您想 sprintf 到缓冲区或类似的缓冲区,可以将整数直接视为 char。事实上,您可能想让您的函数返回一个 char,以使发生的情况变得显而易见。

In C, a char is basically an int between 0 and 255, so you can treat the integer directly as a char if you want to sprintf to a buffer or similar. You might want to make your function return a char, in fact, to make it obvious what's going on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文