Advertisement
Guest User

twete

a guest
Oct 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.82 KB | None | 0 0
  1. #include <windows.h>
  2. #include <iostream>
  3. #include <math.h>
  4. #include <algorithm>
  5. #include<string>
  6. #include<sstream>
  7. using namespace std;
  8. string to_string(float i)
  9. {
  10. stringstream ss;
  11. ss << i;
  12. return ss.str();
  13. }
  14. string to_string(int i)
  15. {
  16. stringstream ss;
  17. ss << i;
  18. return ss.str();
  19. }
  20. HWND text_box1; // declare before the switch otherwise you have cross initialization
  21. HWND text_box2; // declare before the switch otherwise you have cross initialization
  22. HWND button1;
  23. HWND button2;
  24. HWND text_box3;
  25. HWND text_box4;
  26. HWND button3;
  27. HWND button4;
  28.  
  29. void convert_to_bin(int myint, char buff[])
  30. { cout << myint << endl;
  31. int index=1;
  32. for (int j=0;j<=31;j++)
  33. { if ( ( myint & index ) !=0 ) buff[31-j]='1'; else buff[31-j]='0';
  34. index=index<<1;
  35. }//for
  36. cout << "My bin is : " << buff << endl;
  37. }// convert operation
  38. int back_to_int(char buff[])
  39. {
  40. int digit,num=0;
  41. for (int j=1;j<=31;j++)
  42. { if (buff[j]=='1') digit=1; else digit=0;
  43. num=2*num+digit;
  44. }
  45. if (buff[0]=='1'){ num=num+(int)pow(2,31)+1; } // negative numbers
  46. return num;
  47. }//Back to int
  48. void DisplayFloatRepresentation(float b, char c[])
  49. {
  50. int *floatBits = reinterpret_cast<int*> (&b);
  51. for (int i = 0; i <= 31; i++)
  52. c[i] = ((*floatBits >> i) & 1) + '0';//'>>' shifts index to left same to index*2 and compares each bit with 1 using '&' operator
  53. cout << "The float entered in binary is : ";
  54. for (int j = 31; j >= 0; j--)
  55. cout << c[j];
  56. c[32] = '\0';
  57. cout << endl;
  58. }
  59. //float to binary
  60. float CharArrtoFloat(char buff[])
  61. {
  62. int ex = 0;
  63. double sum = 0;
  64. char sign;
  65. float number;
  66. int temp;
  67. for (int j = 30, i = 7; j >= 23; j--, i--)//loop to calculate the number to be subtracted by 127
  68. {
  69. temp = buff[j];
  70. temp = temp / 49;
  71. sum = (pow(2, i)*temp) + sum; //the loop starts at 2^7, where i=7 and i decrements till 2^0. Each is multiplied with a bit and added.
  72. }
  73. ex = sum - 127;//the number from the above loop subtracted by 127 to get the exponent
  74. sum = 0;//sum reset to 0
  75. for (int j = 22, i = -1; j >= 0; j--, i--)
  76. {
  77. temp = buff[j];
  78. temp = temp / 49;
  79. sum = ((pow(2, i)) * temp) + sum;//similar to previous loop, starting from 2^-1 till the remaining 23 bits
  80. }
  81. sum = sum + 1;//1 is added to the sum to account for the invisible dot which is 2^0
  82. number = sum * (pow(2, ex));//final number calculated by multiplying sum 2^exponent
  83. if (buff[31] == '1')
  84. {
  85. sign = '-';// assign '-' if sign bit 1
  86. number *= -1;
  87. }
  88. else sign = '+';// assign + if sign bit 0
  89. cout << "Binary Float to Number is: " << sign << number << endl;
  90. return number;
  91. }
  92. //Back to float
  93.  
  94. /* Declare Windows procedure */
  95. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  96.  
  97. /* Make the class name into a global variable */
  98. char szClassName[ ] = "WindowsApp";
  99.  
  100. int WINAPI WinMain (HINSTANCE hThisInstance,
  101. HINSTANCE hPrevInstance,
  102. LPSTR lpszArgument,
  103. int nFunsterStil)
  104.  
  105. {
  106. HWND hwnd; /* This is the handle for our window */
  107. MSG messages; /* Here messages to the application are saved */
  108. WNDCLASSEX wincl; /* Data structure for the windowclass */
  109.  
  110. /* The Window structure */
  111. wincl.hInstance = hThisInstance;
  112. wincl.lpszClassName = szClassName;
  113. wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
  114. wincl.style = CS_DBLCLKS; /* Catch double-clicks */
  115. wincl.cbSize = sizeof (WNDCLASSEX);
  116.  
  117. /* Use default icon and mouse-pointer */
  118. wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  119. wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  120. wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  121. wincl.lpszMenuName = NULL; /* No menu */
  122. wincl.cbClsExtra = 0; /* No extra bytes after the window class */
  123. wincl.cbWndExtra = 0; /* structure or the window instance */
  124. /* Use Windows's default color as the background of the window */
  125. wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  126.  
  127. /* Register the window class, and if it fails quit the program */
  128. if (!RegisterClassEx (&wincl))
  129. return 0;
  130.  
  131. /* The class is registered, let's create the program*/
  132. hwnd = CreateWindowEx (
  133. 0, /* Extended possibilites for variation */
  134. szClassName, /* Classname */
  135. "Binary conversions for int and float",
  136. WS_OVERLAPPEDWINDOW, /* default window */
  137. CW_USEDEFAULT, /* Windows decides the position */
  138. CW_USEDEFAULT, /* where the window ends up on the screen */
  139. 700, /* The programs width */
  140. 375, /* and height in pixels */
  141. HWND_DESKTOP, /* The window is a child-window to desktop */
  142. NULL, /* No menu */
  143. hThisInstance, /* Program Instance handler */
  144. NULL /* No Window Creation data */
  145. );
  146.  
  147. /* Make the window visible on the screen */
  148. ShowWindow (hwnd, nFunsterStil);
  149.  
  150. /* Run the message loop. It will run until GetMessage() returns 0 */
  151. while (GetMessage (&messages, NULL, 0, 0))
  152. {
  153. /* Translate virtual-key messages into character messages */
  154. TranslateMessage(&messages);
  155. /* Send message to WindowProcedure */
  156. DispatchMessage(&messages);
  157. }
  158.  
  159. /* The program return-value is 0 - The value that PostQuitMessage() gave */
  160. return messages.wParam;
  161. }
  162.  
  163.  
  164. /* This function is called by the Windows function DispatchMessage() */
  165.  
  166. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  167. {
  168. int i_text1=1;
  169. int i_text2=2;
  170. int i_button1=3;
  171. int i_button2=4;
  172. int i_text3 = 5;
  173. int i_text4 = 6;
  174. int i_button3 = 7;
  175. int i_button4 = 8;
  176. char cleartext [1];
  177. cleartext [1] = '\0';
  178.  
  179. switch (message) /* handle the messages */
  180. {
  181. case WM_DESTROY:
  182. PostQuitMessage (0); /* send a WM_QUIT to the message queue */
  183. break;
  184. case WM_CREATE: // after case WM_destroy
  185. // 1. Creating Text box
  186.  
  187. text_box1= CreateWindow(TEXT("EDIT"), TEXT("Int"),WS_VISIBLE
  188. | WS_BORDER | WS_CHILD |ES_AUTOHSCROLL,
  189. 50,30,150,25,hwnd, (HMENU)i_text1, NULL,NULL);
  190. text_box2= CreateWindow(TEXT("EDIT"), TEXT("Binary "),WS_VISIBLE
  191. | WS_BORDER | WS_CHILD |ES_AUTOHSCROLL,
  192. 300,30,300,25,hwnd, (HMENU)i_text2, NULL,NULL);
  193. button1= CreateWindow(TEXT("BUTTON"), TEXT("To Binary"),WS_VISIBLE
  194. | WS_BORDER | WS_CHILD,
  195. 50,100,80,25,hwnd, (HMENU)i_button1, NULL,NULL);
  196. button2= CreateWindow(TEXT("BUTTON"), TEXT("Back to Int"),WS_VISIBLE
  197. | WS_BORDER | WS_CHILD,
  198. 300,100,80,25,hwnd, (HMENU)i_button2, NULL,NULL);
  199.  
  200.  
  201. text_box3= CreateWindow(TEXT("EDIT"), TEXT("Float"),WS_VISIBLE
  202. | WS_BORDER | WS_CHILD |ES_AUTOHSCROLL,
  203. 50,150,150,25,hwnd, (HMENU)i_text3, NULL,NULL);
  204. text_box4= CreateWindow(TEXT("EDIT"), TEXT("Binary "),WS_VISIBLE
  205. | WS_BORDER | WS_CHILD |ES_AUTOHSCROLL,
  206. 300,150,300,25,hwnd, (HMENU)i_text4, NULL,NULL);
  207. button3= CreateWindow(TEXT("BUTTON"), TEXT("To Binary"),WS_VISIBLE
  208. | WS_BORDER | WS_CHILD,
  209. 50,230,80,25,hwnd, (HMENU)i_button3, NULL,NULL);
  210. button4= CreateWindow(TEXT("BUTTON"), TEXT("Back to float"),WS_VISIBLE
  211. | WS_BORDER | WS_CHILD,
  212. 300,230,100,25,hwnd, (HMENU)i_button4, NULL,NULL);
  213.  
  214. break;
  215. case WM_COMMAND:
  216. if (LOWORD(wParam)==i_button1)
  217. {MessageBox(hwnd,"int conversion","title",
  218. MB_OK|MB_ICONINFORMATION);
  219. char my_int_text [80];
  220. char buff[33];
  221. buff[32] = '\0';
  222. int bufsize = GetWindowTextLength(text_box1) ;
  223. int i=GetWindowText(text_box1,(char *)my_int_text,bufsize + 1) ;
  224. int my_int = atoi(my_int_text);
  225. cout << my_int << endl;
  226. convert_to_bin(my_int,buff);
  227. SetWindowText(text_box2, buff);
  228. SetWindowText(text_box1, cleartext);
  229. }//Button 1
  230.  
  231. if (LOWORD(wParam)==i_button2)
  232. {
  233. char buff[33];
  234. buff[32] = '\0';
  235. int bufsize = GetWindowTextLength(text_box2);
  236. int i=GetWindowText(text_box2 ,(char *)buff,bufsize + 1) ;
  237. int my_int = back_to_int(buff);
  238. string int_text;
  239. int_text = to_string(my_int);
  240. SetWindowText(text_box1, int_text.c_str());
  241. }//Button 2
  242. if (LOWORD(wParam)==i_button3)
  243. {MessageBox(hwnd,"Float converstion","title",
  244. MB_OK|MB_ICONINFORMATION);
  245. char my_float_text [80];
  246. char buff[33];
  247. buff[32] = '\0';
  248. int bufsize = GetWindowTextLength(text_box3);
  249. int i=GetWindowText(text_box3,(char *)my_float_text,bufsize + 1) ;
  250. float my_float = atof(my_float_text);
  251. cout<<my_float<<endl;
  252. DisplayFloatRepresentation(my_float, buff);
  253. char buff1[33]={'/0'};
  254. for(int i = 0,j = 31 ; i <=31; i++, j--)
  255. {
  256. buff1[i] = buff[j];
  257. }
  258. buff1[32] = '\0';
  259. SetWindowText(text_box4, buff1);
  260. SetWindowText(text_box3, cleartext);
  261. }//Button 3
  262. if (LOWORD(wParam)==i_button4)
  263. {
  264. char buff[32];
  265. buff[32] = '\0';
  266. float bufsize = GetWindowTextLength(text_box4);
  267. int i=GetWindowText(text_box4 ,(char *)buff,bufsize + 1);
  268. char buff1[32];
  269. for(int i = 0,j = 31 ; i < 32; i++, j--)
  270. {
  271. buff1[i] = buff[j];
  272. }
  273. buff1[32] = '\0';
  274. cout<<"The float in Binary is : "<<buff1<<endl;
  275. float my_float = CharArrtoFloat(buff1);
  276. char float_text[10];
  277. string my_stringFloat=to_string(my_float);
  278. cout<<my_stringFloat<<endl;
  279. for(int i=0;i<=9;i++)
  280. {
  281. float_text[i]=my_stringFloat[i];
  282. }
  283. SetWindowText(text_box3, float_text);
  284. }//Button 4
  285. break;
  286. default:
  287. /* for messages that we don't deal with */
  288. return DefWindowProc (hwnd, message, wParam, lParam);
  289. }
  290.  
  291. return 0;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement