需要随机数表,自己做个程序生成*.c *.bin

/*
rram.c
用于生成随机数组
不支持M级
例:
rram 1024
rram 1 k
rram 1 K

rram 2048
rram 2 k
rram 2 K

*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void print_info()
{
   
printf("\
    Usage:rram [count] [units]\n\
    -count of data by units\n\
    -units Bb Kk, Bb is the default for no units given\n"
);
}
void print_err(int err)
{
   
char* errmsg[]={
   
"memory alloc failure",/*0*/
   
"file open failure"    /*1*/
   
};
   
if(err<sizeof(errmsg)/sizeof(char*))
    {
       
puts(errmsg[err]);
    }
    
    
}
void  dump_data(unsigned char* p,size_t size)
{
   
int i=0;
   
while(size--)
    {
       
printf("0x%02x ",*p++);
       
if(i++==7)
        {
           
printf("\n");
           
i=0;
        }
    }
}
void memrand(unsigned char* p,size_t size)
{
   
srand((unsigned)time(NULL));
   
while(size--)
        *
p++=rand();
    
}
void data2c(unsigned char* p,size_t size)
{
   
FILE* fp;
   
int i;
   
int j;
   
if(fp=fopen("randram.c","w"))
    {
        
       
fprintf(fp,"unsigned char rand_bytes[%d]={\n",size);
       
i=size;
       
j=0;
       
while(i--)
        {
           
if(i)
               
fprintf(fp,"0x%02x,",*p++);
           
else
               
fprintf(fp,"0x%02x};",*p++);
           
if(j++==15)
            {
               
fprintf(fp,"\n");
               
j=0;
            }
            
            
        }
        
       
fclose(fp);
    }
   
else
   
{
       
print_err(1);
       
exit(0);
    }
    
}
void data2bin(unsigned char* p,size_t size)
{
   
FILE* fp;
   
int i;
   
int j;
   
if(fp=fopen("randram.bin","wb"))
    {
        
       
fwrite(p,size,1,fp);
        
       
fclose(fp);
    }
   
else
   
{
       
print_err(1);
       
exit(0);
    }
}
int main(int argc,char* argv[])
{
   
/*vars*/
   
size_t count;

   
int i,j=0;
    
   
unsigned char * pdata;
    
    
   
/*arg convert*/
   
if(argc!=2 && argc!=3)
    {
       
print_info();
       
exit(0);
    }
   
count=atol(argv[1]);
   
if(count==0)
    {
       
print_info();
       
exit(0);
    }
   
if(argc==3)
    {    
       
if(0==strcmp(argv[2],"K") || 0==strcmp(argv[2],"k"));
           
count*=1024;
    }
   
pdata=(unsigned char*)malloc(count);
   
if(pdata)
       
memrand(pdata,count);
   
else
   
{
       
print_err(0);
       
exit(0);
    }

   
dump_data(pdata,count);
   
data2c(pdata,count);
   
data2bin(pdata,count);
   
free(pdata);    
   
return 0;
    
}

posted on 2009-08-07 21:29  devcfei  阅读(400)  评论(0编辑  收藏  举报