473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Converting a .wav file to binary strings

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
1 5625
DeMan
1,806 1GB
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

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

Similar topics

11
by: Laurent Therond | last post by:
Maybe you have a minute to clarify the following matter... Consider: --- from cStringIO import StringIO def bencode_rec(x, b): t = type(x)
5
by: nickisme | last post by:
Hi - sorry for the possibly stupid question, but I'm still a wee starter on c++... Just wondering if there's a quick way to convert data into binary strings... To explain, I'm trying to convert...
2
by: Pete | last post by:
Sorry for the ambiguity of my last post, What I am try to do is enter a 10 bit binary string eg: 1110001010 and then permute them into an array using an array containing 3,5,2,7,4,10,1,9,8,6 as...
3
by: ruben.de.visscher | last post by:
I am trying to write a program that encrypts 8-bit plaintext using a 10-bit key. To generate subkeys, and for other things, i will need to be able to perform permutations on binary strings for...
8
by: Patrik Malmström | last post by:
How do I read, write a file binary? I want to open, say, file.exe read it in to the program, then write it out to file2.exe. Like file copy, anyone have a code sample?
9
by: ruffiano | last post by:
Hi, can someone tell me if a C++ string (std::string) represents a binary or an ASCII string? Thanks in advance.
2
by: CharlesL | last post by:
I am trying to handle binary strings in php. I get a binary output initialization vector from mcrypt as such: from mcrypt: $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); This output may have...
4
by: nguser3552 | last post by:
Hello everyone, I'm wondering if someone out there knows how in a visual c++ console application how I can do the following, and man I've tried, it seems simple really: I need to open up any...
0
by: Terry Reedy | last post by:
A. Joseph wrote: These are number representation systems that can be applied to or used with integral, rational (numberator,denominator), and 'point' numbers. Try Wikipedia or any search...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.