473,732 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting a .wav file to binary strings

1 New Member
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(presuma bly 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 5692
DeMan
1,806 Top Contributor
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
2947
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
2222
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 numbers into 16 bit binary form, so that I can eventually alter the LSB for dithering purposes. Obviously I can create my own function that will do this, but I was wondering if there was some quick process that I could call up from a library that...
2
1707
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 the index, so the 1st bit will be in the P10_out, 2nd bit in P10_out and so on. I am sure the problem is to do with the input string, it is each 1 and 0 of the input is not being read as individual elements. Your help is much appreciated. ...
3
2755
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 example: if k1k2k3k4k5k6k7k8k9k10 represents all ten digits of the key, the outpur of the permutation should be like this: k3k5k2k7k4k10k1k9k8k6 . I am new to operations on binary strings and i don't really know how to implement this as...
8
31011
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
1954
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
2740
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 padded nulls at the end which are significant. I would like to convert all characters including all padding nulls at end and convert it in a string to be saved into a database in such a way
4
1906
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 file (the fnctl library offers hope), in binary or raw mode just 1' and 0's whichever is preferable. Once the file is open, attached to a stream and a buffer is created, I have to be able to take the final object as though
0
3347
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 engine. There are standard algorithms for converting between representations. See above. Good programmer calculators have these built in.
0
8774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.