By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,301 Members | 1,402 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,301 IT Pros & Developers. It's quick & easy.

Converting a .wav file to binary strings

P: 1
First off my programming experience is very limited and I haven't used C/C++ in the past 4/5 years so I'm fairly c**p at it.

Basically I'm trying to write a function that opens a .wav file and store the binary version of this file in a variable named 'input'. 'input' is a [num]x[16] integer array. 'num' is the no. of bits in the array divided by 16 (rounded up). The following is my code:

Expand|Select|Wrap|Line Numbers
  1. using namespace std;
  2.  
  3. FILE *fp;
  4.  
  5. int main(int argc, char *argv[]) 
  6. {
  7.     int *inp, num, i=0, j=0, k=0;
  8.     long size;
  9.     if((fp=fopen("out.wav","rb"))==NULL) {
  10.     printf("cannot open file\n");
  11.     exit(1);
  12.     }
  13.  
  14.  
  15.     fseek (fp, 0, SEEK_END);
  16.     size= ftell(fp);
  17.     printf("size of my file is %ld bits\n",size);
  18.     fseek (fp, SEEK_CUR, SEEK_SET);
  19.  
  20.     inp = (int *) calloc(size, sizeof(int));
  21.  
  22.     fread(inp,size,1,fp);
  23.     fclose(fp);   
  24.     //printf("%f", inp);
  25.     /*
  26.     for(int i=0; i<size; i++)
  27.     {    
  28.          printf("%d", inp[i]);     
  29.     }
  30.     */
  31.     printf("\n");
  32.  
  33.     num = size/16;
  34.  
  35.  
  36.     if(size%num!=0)
  37.     num++;
  38.     printf("The size of this file can be represented by %d hexadecimal numbers \n", num);
  39.     int input[num][16];
  40.  
  41.     k=0;
  42.  
  43.     for(i=0; i<num; i++)
  44.     {
  45.          for(j=0; j<16; j++)
  46.          {
  47.               (input)[i][j] = (*inp+k);//[k];
  48.               k++;
  49.          }
  50.  
  51.     }  
  52.                  printf("\nThe following is the third input:\n");
  53.                  for(int jj=0; jj<16; jj++)
  54.                  {
  55.  
  56.                           if (jj%4!=0)
  57.                           {
  58.                                      //printf(" %d",input[3][jj]);
  59.                                      printf(" %d",input[3][jj]);
  60.                           }
  61.                           else
  62.                           {
  63.                               //printf("      %d",input[3][jj]);
  64.                               printf(" %d",input[3][jj]);
  65.                           }
  66.                  }
  67.                  printf("\n\n");
  68.  
  69.  
  70.  
  71.     //run encryption code for all arrays!
  72.  
  73.     system("PAUSE");
  74.     return EXIT_SUCCESS;
  75. }
  76.  
When I compile the above code and I printf one of the rows of the variable input the screen prints out 16 10-digit numbers(presumably that's the address). I'm expecting input to be a matrix with rows of size 16 containing 1's and 0's. If anyone can suggest where I might be going wrong it would be much appreciated.
Mar 22 '07 #1
Share this Question
Share on Google+
1 Reply


DeMan
100+
P: 1,806
Consider
The line:
Expand|Select|Wrap|Line Numbers
  1.  (input)[i][j] = (*inp+k);//[k]; 
  2.  
store at input[i][j] the Value at inp with k added.

Firstly, you would want *(inp+k) // the value k positions beyond inp.

Secondly input is an array of integers, so the value at this location is 32 bits (not 1). Furthermore, the compiler tries to be clever and (inp+k) is actually k int-lengths beyond inp, not k bits.

Using the same sort of array you already have, you could create a function that creates binary values by :

input[a][0] == (value >> 16) &1;
input[a][1] == (value >> 15) & 1;
input[a][2] == (value >> 14) & 1;

(ie right shift x bits (and use logical and to only worry about the last bit);

Or there is probably a way to output binary (You might like to see if this link helps you at all.

Good luck!
Mar 22 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.