daily pastebin goal
20%
SHARE
TWEET

Least Significant Bit

a guest Mar 8th, 2010 277 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <sys/stat.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <stdio.h>
  5. using namespace std;
  6. // Developed by: M. Robertson
  7. //Code to derive the Least Significant Bit from a 16bit based file.
  8. // An example of this code is as follows:
  9. //sokalli2      thaipos: if you have say an array of 8 bytes and a variable bits then
  10. // for (int i=0; i<8; ++i) bits = (bits << 1) | (bytes[i] & 1);
  11. int main ()
  12. {
  13.         int dataStart = 40; //This is the 'start' of the data of our file.
  14.                                                 // If your header length is 44 bytes, this should be 44,
  15.                                                 //  due to index start of 0.
  16.                                                 //It would also be wise to open up a hex editor to find the
  17.                                                 // specific spot the data starts. For this file, despite
  18.                                                 // being a .wav, it was at the 40th byte.
  19.         int dataEnd = 3000000; //Put this to a particularly high number
  20.                                                         //if you don't feel like finding out the filesize.
  21.         int byteLength = 0;
  22.         char bits;
  23.         //Make SURE this file is placed wherever the code is being executed from.
  24.         fstream myFile("dinosaur_noise.wav", ios::in | ios::out | ios::binary);
  25.     ofstream saveFile("dinoNoise_LSB");
  26.         char ch;
  27.         cout << "Least Significant Bit extractor for 16bit based files.\n";
  28.  
  29.         //TODO: need to put in an if statement to make sure the file exists.
  30.  
  31.         /*/Uncomment to grab the header...
  32.         for(int i = 0; i < dataStart; i++){
  33.                 myFile.seekg(i);
  34.                 myFile.get(ch);
  35.                 saveFile << ch;
  36.         }//*/
  37.  
  38.         //This loop goes through the entire file / dataSet as designated above.
  39.         for(int i = dataStart; i < dataEnd; i++){
  40.                 myFile.seekg(i);//This forces the index of the currently viewed byte
  41.                 myFile.get(ch); //This pulls the currently viewed byte into the ch variable
  42.  
  43.                 //We check at our current index to see if we're the 16th bit in.
  44.                 // However, we must subtract our dataStart to compensate that we may not
  45.                 // be starting at 0, and +1 due to the nature of C++ starting its file stream
  46.                 // at index 0.
  47.                 if(((i-dataStart)+1)%16 == 0){
  48.                         cout << i << endl;
  49.                         //The byte doesn't necessarily need to be reset,
  50.                         // but is good practice to prevent strange data occurances.
  51.                         bits = 0x00;
  52.                         byteLength++; //Keep a count for how many times we delve in to this.
  53.                                         // This will count the number of bytes total in the program.
  54.                         for(int j = 15; j >= 0; j--){
  55.                                 j--; //Comment this line, change to %8, and j = 7 to grab for 8bit.
  56.                                 myFile.seekg(i-j);
  57.                                 myFile.get(ch);
  58.                                 //cout << ch << "--";                   //Some debug; don't show unless
  59.                                 //cout << (ch&0x01) << endl;    //you're only pulling a couple of bytes.
  60.                                 //saveFile << ch;                               //Also for debugging purposes.
  61.  
  62.                                 //The magic bitwise:
  63.                                 // bits << 1 : Every time a bit is added, move all the bits over 1.
  64.                                 // ch & 1    : Grab the last bit from the char byte.
  65.                                 // | (or) after bit shift and grabbing the last bit adds in a single bit
  66.                                 // to the end of the byte total.
  67.                                 bits = (bits << 1) | (ch & 1);
  68.                         }
  69.                         //Save our totaled byte to our file.
  70.                         saveFile << bits;
  71.                 }
  72.  
  73.                 //cout << (ch);
  74.                
  75.                 //This is to allow a larger number for the for loop than the file.
  76.                 // If we reach the end of the file, simply break out.
  77.                 if(myFile.eof()){
  78.                         cout << ch;
  79.                         break;
  80.                 }
  81.         }
  82.         cout << "\nSuccess. The File is " << byteLength << " bytes long.";
  83.         saveFile.close();
  84.         myFile.close();
  85.         char anything;
  86.         cin >> anything;
  87.         return 0;
  88. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top