Advertisement
stoneharry

Untitled

Sep 23rd, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1.         internal void WriteFourCC(string fourCC)
  2.         {
  3.             byte[] arr = Encoding.ASCII.GetBytes(fourCC);
  4.             Array.Reverse(arr);
  5.  
  6.             if (arr.Length < 4)
  7.             {
  8.                 byte[] backup = arr;
  9.                 arr = new byte[arr.Length + 1];
  10.                 backup.CopyTo(arr, 0);
  11.             }
  12.             int test = BitConverter.ToInt32(arr, 0);
  13.             WriteInt32(BitConverter.ToInt32(arr,0));
  14.         }
  15.  
  16.         internal void WriteInt32( int intVal )
  17.         {
  18.             WriteBits(intVal, 32);
  19.         }
  20.  
  21.         // passing 5730135 into function, which is equal to 'WoW' (WoW + chr(0) to make 4 chars)
  22.         internal void WriteBits<T>(T valz, int numBits)
  23.         {
  24.             if (numBits > 0)
  25.             {
  26.                 // becomes 43 (+ 32 to old result)
  27.                 _numBits += numBits;
  28.                 while (true)
  29.                 {
  30.                     // WritePos = 11, pos7 becomes 3
  31.                     // second run, WritePos = 16, pos7 = 0
  32.                     // 3rd run, WritePos = 32, pos7 = 0
  33.                     // 4th run, WritePos = 40, pos7 = 0
  34.                     // 5th run, WritePos = 40, pos7 = 0
  35.                     int pos7 = (WritePos & 7);
  36.                     // unk = 5
  37.                     // second run, unk = 8
  38.                     // 3rd + 4th + 5th, unk = 8
  39.                     int unk = 8 - pos7;
  40.                     // lShift = 32
  41.                     // second run, 256
  42.                     // 3rd, 4th, 5th = 256
  43.                     var lShift = (char) (1 << unk);
  44.                     int subNum;
  45.  
  46.                     if (unk < numBits)
  47.                         // = 5
  48.                         // second run, 8
  49.                         // 3rd = 8
  50.                         // 4th = 8
  51.                         subNum = unk;
  52.                     else
  53.                     {
  54.                         // 5th run
  55.                         // lshift = 8
  56.                         lShift = (char) (1 << numBits);
  57.                         // subnum = 3
  58.                         subNum = numBits;
  59.                     }
  60.                     // numbits = 27 now
  61.                     // second run, 19
  62.                     // 4th = 3
  63.                     // 5th = 0
  64.                     numBits -= subNum;
  65.                    
  66.                     // 65287
  67.                     // 65280 second run & 3rd & 4th
  68.                     // 65528 = 5th
  69.                     var firstHalf = (char) (~((lShift - 1) << pos7));
  70.  
  71.                     // both 0
  72.                     // second run, both 10
  73.                     // 3rd run = 2797
  74.                     // 4th run = 60906
  75.                     // 5th run = 28503
  76.                     var shifted = (char) Shift(valz, numBits);
  77.                     // 3rd run = 237
  78.                     // 4th run = 234
  79.                     // 5th run = 7
  80.                     var secondHalf = (char) (((lShift - 1) & shifted) << pos7);
  81.  
  82.                     Buffer[WritePos >> 3] = (byte) (Buffer[WritePos >> 3] & firstHalf | secondHalf);
  83.  
  84.                     // writepos = 16
  85.                     // second run, writepos = 24
  86.                     // 3rd run, = 32
  87.                     // 4th run = 40
  88.                     // 5th run = 43
  89.                     WritePos += subNum;
  90.  
  91.                     // 5th run = 0
  92.                     if (numBits == 0)
  93.                         return;
  94.                 }
  95.             }
  96.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement