Random hex code generation saving

Sep 29, 2022 at 2:43pm
Hi,

I'm trying to generate a hex random generators and catch it in 2d array.
ex o/p random hex:

0x12 0xff 0x34
0x3e 0x00 0xef
0x01 0x43 0xb3
..
..

so far in C, some basic

#include<stdio.h>
main()
{
char buf[3][4] ={0};
srand(time(NULL));
char p[5] = {0};

for(int i =0; i<3;i++)
{
for(int j =0; j< 4; j++)
{
sprintf(p, "0x%02x", rand()%256); // here i screwed when i use buf[i][j]
// any suggestions how to do. or any better method
}

}
return 0;
}

Thanks in advance
Last edited on Sep 29, 2022 at 2:47pm
Sep 29, 2022 at 3:16pm
Well your buff only contains 3x4 chars, not 3x4 strings of char[5]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
int main()
{
  char buf[3][4][5] ={0};
  srand(time(NULL));
  for(int i=0; i<3; i++)
  {
    for(int j=0; j<4; j++)
    {
      sprintf(buff[i][j], "0x%02x", rand()%256);
    }
  }
  return 0;
}



new1 (46) - and still not using [code][/code] tags.
Last edited on Sep 29, 2022 at 3:17pm
Sep 29, 2022 at 3:26pm
oh i didn't give each one size yeah. Thanks for pointing out.

i tried to select code tags thru format option but not shown. I'll follow this. :)

Is there any better way for generation of hex and store.?
Sep 29, 2022 at 4:20pm
A post without code tags can be edited to add them. :)

https://cplusplus.com/articles/jEywvCM9/

There are other tags available you can use.

https://cplusplus.com/articles/z13hAqkS/

With new topic opening posts the format buttons aren't reliably usable. Follow-up posts can use the format buttons.

The limitations of a free forum.
Sep 29, 2022 at 4:34pm
FYI, new1, according the C & C++ ISO standards main's return type must be int, never blank or void.

https://stackoverflow.com/questions/17715008/the-return-type-of-main-function/17715054#17715054

There are compilers that allow this, but anything other than int main is not a conforming implementation. If you want portable code use int main.
Sep 29, 2022 at 5:09pm
Is there any better way for generation of hex and store.?


if its meant to be c++ code, then yes, you have <random> which is a far better generator tool.
if its C, as written, an alternate random library would outperform rand() which is recommended to be replaced for any serious code.
Sep 29, 2022 at 5:47pm
Is there any better way for generation of hex

As jonnin mentions if using C++ there's the C++ stdlib <random> library. Still not the best random number generators available, none of the C++ engines are cryptographically secure. There are 3rd party libraries that are if needed.

Finding an alternative for C requires going 3rd party if you want to avoid <stdlib.h>.

https://stackoverflow.com/questions/9492581/c-random-number-generation-pure-c-code-no-libraries-or-functions

There's a suggestion for a linear congruential generator:

https://www.cs.wm.edu/~va/software/park/park.html

Warning: the compressed .tgz file is borked, you need to download the 4 separate individual files themselves.

I can't say if those source files work, I haven't tested them in any code. I just downloaded them.

There are a number of other C library replacement suggestions as well at the SO link.
Topic archived. No new replies allowed.