c - Generate large array of Hexadecimal numbers of size more that 8 MB -
i trying generate large array of hexadecimal numbers , numbers randomly generated. size of array ranges 8 mb 40 mb. should implement in c. know should use malloc creating large array, couldn't figure out how generate random hexadecimal numbers , put in array. constraint before every hexadecimal number there should "0x". if use
str[i] = hex_digits[ ( rand() % 16 ) ];
i generate hexadecimal number doesn't have 0x in front of it. , don't know how use pointer. not in programming. can please guide me this..!!
to make clear, giving array input in aes implementation in c. in code have, there 10 hexadecimal numbers input, have try large inputs 8 mb, 16 mb, 32 mb , 64 mb.
i tried code , started implementing on simple array. code following:
#include <stdio.h> void main() { char a[10]; int i,j,k; for(i=0;i<2;i++) { j=4*i; printf("%d\n",i); printf("%d\n",j); { a[j]='0'; a[j+1]='x'; a[j+2]='a'; a[j+3]='2'; } } for(k=0;k<8;k++) { printf("%s\n",&a[k]); } }
i thought if works can make large array. trying put "0xa2" twice in array, instead getting following output.
0xa20xa2 xa20xa2 a20xa2 0xa2 xa2 a2 2
any appreciated.
thanks in advance,
raghu.
create random number in usual fashion. when printing, pre-pend "0x".
#include <stdio.h> #include <stlib.h> #include <time.h> void randprint(size_t n) { unsigned char *bigarray = malloc(n); if (bigarray != null) { // add line vary random numbers generated. // simple method, better ones exist. srand(time()); (size_t = 0; < n; i++) { bigarray[i] = rand(); } // if code needs output numbers, print (size_t = 0; < n; i++) { printf("0x%02x ", bigarray[i]); } // if code needs call aes encryption routine aes_encryption(bigarray, n); // not clear on op wants after encryption. free(bigarray); } } //examples randprint(10); randprint(8l*1024*1024);
suggest "0x" not need saved in array nor conversion of integer hexadecimal characters. typically used direct human reading or textually saving/passing data in human readable format.
Comments
Post a Comment