Darker666

double from char*

Mar 17th, 2013
32
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.       else if(packetID==13) {
  2.           char position[3][8];     //X,Y,Z
  3.           cout<<" - Player position: [\n";
  4.           for(int coord=0; coord<3; coord++) {
  5.             scl.read(8, position[coord]);    //Reads int bytes to char* (instance of socket class)
  6.             for(int xx=0; xx<8; xx++)    //just looping through char* to see the contents
  7.                 cout<<(int)position[coord][xx]<<" ";
  8.             cout<<"\n";
  9.             swapBytes(position[coord], 8);   //swaping bytes
  10.             for(int xx=0; xx<8; xx++)       //Checking if swapped correctly
  11.                 cout<<(int)position[coord][xx]<<" ";
  12.  
  13.             cout<<"\n -> "<<(*((double*)position[coord]));
  14.             if(coord<2)    
  15.                 cout<<", \n\n";
  16.           }
  17.           cout<<"]\n";
  18.           if(!scl.ignore(41-3*8))break;   //Ignoring the rest of the data
  19.           break;
  20.          
  21.       }
  22. void swapBytes(char*arr, int length) {
  23.     char b;
  24.     for(int i=0; i<length; i+=2) {
  25.         b = arr[i];
  26.         arr[i] = arr[i+1];
  27.         arr[i+1]=b;
  28.     }
  29. }
  30. /*PROGRAM OUTPUT*/
  31. /*
  32.  - Player position: [
  33. 64 80 43 -3 12 -3 91 -55
  34. 80 64 -3 43 -3 12 -55 91
  35.  -> 1.42249e+134,
  36.  
  37. 64 85 -89 -82 20 -128 0 0
  38. 85 64 -82 -89 -128 20 0 0
  39.  -> 1.11376e-310,
  40.  
  41. 64 85 64 0 0 0 0 0
  42. 85 64 0 64 0 0 0 0
  43.  -> 5.30507e-315]
  44. Over and out!
  45. Last packet: 13
  46.  
  47. actual position is near [63,86,309]
  48. */
RAW Paste Data