如何用随机(十六进制)字符填充字符串?

时间:2022-06-17 22:17:06

I have a string (unsigned char) and i want to fill it with only hex characters.

我有一个字符串(unsigned char),我想用十六进制字符填充它。

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

当然,在运行这个时我会受到分裂

4 个解决方案

#1


7  

  1. string is an array of char-s not unsigned char-s
  2. string是char-s的数组,不是unsigned char-s

  3. you are using str[i] (which is of type unsigned char) as a 1st argument to sprintf, but it requires type char * (pointer).
  4. 你使用str [i](unsigned char类型)作为sprintf的第一个参数,但它需要类型char *(指针)。

This should be a little better:

这应该会好一点:

char str[STR_LEN + 1];
for(i = 0; i < STR_LEN; i++) {
    sprintf(str + i, "%x", rand() % 16);
}

#2


1  

You could try something like this:

你可以尝试这样的事情:

#include <stdio.h>
#include <stdlib.h>

#define STR_LEN 20

int main(void)
{
   unsigned char str[STR_LEN + 1] = {0};
   const char *hex_digits = "0123456789ABCDEF";
   int i;

   for( i = 0 ; i < STR_LEN; i++ ) {
      str[i] = hex_digits[ ( rand() % 16 ) ];
   }

   printf( "%s\n", str );

   return 0;
}

#3


1  

The first argument to sprintf() should be a char*, but str[i] is a char: this is the cause of the segmentation fault. The compiler should have emitted a warning about this. gcc main.c, without specifying a high warning level, emitted the following:

sprintf()的第一个参数应该是char *,但str [i]是char:这是分段错误的原因。编译器应该发出警告。 gcc main.c,没有指定高警告级别,发出以下内容:

warning: passing argument 1 of sprintf makes pointer from integer without a cast

警告:传递sprintf的参数1使整数指针不带强制转换

A hex representation of a character can be 1 or 2 characters (9 or AB for example). For formatting, set the precision to 2 and the fill character to 0. Also need to add one character for the terminating null to str and set the step of the for loop to 2 instead of 1 (to prevent overwriting previous value):

字符的十六进制表示可以是1或2个字符(例如9或AB)。对于格式化,将精度设置为2,将填充字符设置为0.还需要为终止null添加一个字符到str,并将for循环的步骤设置为2而不是1(以防止覆盖以前的值):

unsigned char str[STR_LEN + 1] = {0};
int i;

for (i = 0; i < STR_LEN; i += 2)
{
    sprintf(&str[i], "%02X", rand() % 16);
}

#4


0  

There are several unclarities and problems in your code. I interpret "hex character" to mean "hex digit", i.e. a symbol from {0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f}, not "the hexadecimal value of an ascii character's code point". This might or might not be what you meant.

您的代码中存在一些不清楚和问题。我将“十六进制字符”解释为“十六进制数字”,即来自{0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f}的符号,而不是“ascii字符的代码点的十六进制值”。这可能是也可能不是你的意思。

This should do it:

这应该这样做:

void hex_fill(char *buf, size_t max)
{
  static const char hexdigit[16] = "0123456789abcdef";
  if(max < 1)
    return;
  --max;
  for(i = 0; i < max; ++i)
    buf[i] = hexdigit[rand() % sizeof hexdigit];
  buf[max] = '\0';
}

The above will always 0-terminate the string, so there's no requirement that you do so in advance. It will properly handle all buffer sizes.

以上将永远0终止字符串,因此不要求您提前这样做。它将正确处理所有缓冲区大小。

#1


7  

  1. string is an array of char-s not unsigned char-s
  2. string是char-s的数组,不是unsigned char-s

  3. you are using str[i] (which is of type unsigned char) as a 1st argument to sprintf, but it requires type char * (pointer).
  4. 你使用str [i](unsigned char类型)作为sprintf的第一个参数,但它需要类型char *(指针)。

This should be a little better:

这应该会好一点:

char str[STR_LEN + 1];
for(i = 0; i < STR_LEN; i++) {
    sprintf(str + i, "%x", rand() % 16);
}

#2


1  

You could try something like this:

你可以尝试这样的事情:

#include <stdio.h>
#include <stdlib.h>

#define STR_LEN 20

int main(void)
{
   unsigned char str[STR_LEN + 1] = {0};
   const char *hex_digits = "0123456789ABCDEF";
   int i;

   for( i = 0 ; i < STR_LEN; i++ ) {
      str[i] = hex_digits[ ( rand() % 16 ) ];
   }

   printf( "%s\n", str );

   return 0;
}

#3


1  

The first argument to sprintf() should be a char*, but str[i] is a char: this is the cause of the segmentation fault. The compiler should have emitted a warning about this. gcc main.c, without specifying a high warning level, emitted the following:

sprintf()的第一个参数应该是char *,但str [i]是char:这是分段错误的原因。编译器应该发出警告。 gcc main.c,没有指定高警告级别,发出以下内容:

warning: passing argument 1 of sprintf makes pointer from integer without a cast

警告:传递sprintf的参数1使整数指针不带强制转换

A hex representation of a character can be 1 or 2 characters (9 or AB for example). For formatting, set the precision to 2 and the fill character to 0. Also need to add one character for the terminating null to str and set the step of the for loop to 2 instead of 1 (to prevent overwriting previous value):

字符的十六进制表示可以是1或2个字符(例如9或AB)。对于格式化,将精度设置为2,将填充字符设置为0.还需要为终止null添加一个字符到str,并将for循环的步骤设置为2而不是1(以防止覆盖以前的值):

unsigned char str[STR_LEN + 1] = {0};
int i;

for (i = 0; i < STR_LEN; i += 2)
{
    sprintf(&str[i], "%02X", rand() % 16);
}

#4


0  

There are several unclarities and problems in your code. I interpret "hex character" to mean "hex digit", i.e. a symbol from {0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f}, not "the hexadecimal value of an ascii character's code point". This might or might not be what you meant.

您的代码中存在一些不清楚和问题。我将“十六进制字符”解释为“十六进制数字”,即来自{0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f}的符号,而不是“ascii字符的代码点的十六进制值”。这可能是也可能不是你的意思。

This should do it:

这应该这样做:

void hex_fill(char *buf, size_t max)
{
  static const char hexdigit[16] = "0123456789abcdef";
  if(max < 1)
    return;
  --max;
  for(i = 0; i < max; ++i)
    buf[i] = hexdigit[rand() % sizeof hexdigit];
  buf[max] = '\0';
}

The above will always 0-terminate the string, so there's no requirement that you do so in advance. It will properly handle all buffer sizes.

以上将永远0终止字符串,因此不要求您提前这样做。它将正确处理所有缓冲区大小。