Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <iostream>
- #include <math.h>
- #include <algorithm>
- #include<string>
- #include<sstream>
- using namespace std;
- string to_string(float i)
- {
- stringstream ss;
- ss << i;
- return ss.str();
- }
- string to_string(int i)
- {
- stringstream ss;
- ss << i;
- return ss.str();
- }
- HWND text_box1; // declare before the switch otherwise you have cross initialization
- HWND text_box2; // declare before the switch otherwise you have cross initialization
- HWND button1;
- HWND button2;
- HWND text_box3;
- HWND text_box4;
- HWND button3;
- HWND button4;
- void convert_to_bin(int myint, char buff[])
- { cout << myint << endl;
- int index=1;
- for (int j=0;j<=31;j++)
- { if ( ( myint & index ) !=0 ) buff[31-j]='1'; else buff[31-j]='0';
- index=index<<1;
- }//for
- cout << "My bin is : " << buff << endl;
- }// convert operation
- int back_to_int(char buff[])
- {
- int digit,num=0;
- for (int j=1;j<=31;j++)
- { if (buff[j]=='1') digit=1; else digit=0;
- num=2*num+digit;
- }
- if (buff[0]=='1'){ num=num+(int)pow(2,31)+1; } // negative numbers
- return num;
- }//Back to int
- void DisplayFloatRepresentation(float b, char c[])
- {
- int *floatBits = reinterpret_cast<int*> (&b);
- for (int i = 0; i <= 31; i++)
- c[i] = ((*floatBits >> i) & 1) + '0';//'>>' shifts index to left same to index*2 and compares each bit with 1 using '&' operator
- cout << "The float entered in binary is : ";
- for (int j = 31; j >= 0; j--)
- cout << c[j];
- c[32] = '\0';
- cout << endl;
- }
- //float to binary
- float CharArrtoFloat(char buff[])
- {
- int ex = 0;
- double sum = 0;
- char sign;
- float number;
- int temp;
- for (int j = 30, i = 7; j >= 23; j--, i--)//loop to calculate the number to be subtracted by 127
- {
- temp = buff[j];
- temp = temp / 49;
- 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.
- }
- ex = sum - 127;//the number from the above loop subtracted by 127 to get the exponent
- sum = 0;//sum reset to 0
- for (int j = 22, i = -1; j >= 0; j--, i--)
- {
- temp = buff[j];
- temp = temp / 49;
- sum = ((pow(2, i)) * temp) + sum;//similar to previous loop, starting from 2^-1 till the remaining 23 bits
- }
- sum = sum + 1;//1 is added to the sum to account for the invisible dot which is 2^0
- number = sum * (pow(2, ex));//final number calculated by multiplying sum 2^exponent
- if (buff[31] == '1')
- {
- sign = '-';// assign '-' if sign bit 1
- number *= -1;
- }
- else sign = '+';// assign + if sign bit 0
- cout << "Binary Float to Number is: " << sign << number << endl;
- return number;
- }
- //Back to float
- /* Declare Windows procedure */
- LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
- /* Make the class name into a global variable */
- char szClassName[ ] = "WindowsApp";
- int WINAPI WinMain (HINSTANCE hThisInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpszArgument,
- int nFunsterStil)
- {
- HWND hwnd; /* This is the handle for our window */
- MSG messages; /* Here messages to the application are saved */
- WNDCLASSEX wincl; /* Data structure for the windowclass */
- /* The Window structure */
- wincl.hInstance = hThisInstance;
- wincl.lpszClassName = szClassName;
- wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
- wincl.style = CS_DBLCLKS; /* Catch double-clicks */
- wincl.cbSize = sizeof (WNDCLASSEX);
- /* Use default icon and mouse-pointer */
- wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
- wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
- wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
- wincl.lpszMenuName = NULL; /* No menu */
- wincl.cbClsExtra = 0; /* No extra bytes after the window class */
- wincl.cbWndExtra = 0; /* structure or the window instance */
- /* Use Windows's default color as the background of the window */
- wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
- /* Register the window class, and if it fails quit the program */
- if (!RegisterClassEx (&wincl))
- return 0;
- /* The class is registered, let's create the program*/
- hwnd = CreateWindowEx (
- 0, /* Extended possibilites for variation */
- szClassName, /* Classname */
- "Binary conversions for int and float",
- WS_OVERLAPPEDWINDOW, /* default window */
- CW_USEDEFAULT, /* Windows decides the position */
- CW_USEDEFAULT, /* where the window ends up on the screen */
- 700, /* The programs width */
- 375, /* and height in pixels */
- HWND_DESKTOP, /* The window is a child-window to desktop */
- NULL, /* No menu */
- hThisInstance, /* Program Instance handler */
- NULL /* No Window Creation data */
- );
- /* Make the window visible on the screen */
- ShowWindow (hwnd, nFunsterStil);
- /* Run the message loop. It will run until GetMessage() returns 0 */
- while (GetMessage (&messages, NULL, 0, 0))
- {
- /* Translate virtual-key messages into character messages */
- TranslateMessage(&messages);
- /* Send message to WindowProcedure */
- DispatchMessage(&messages);
- }
- /* The program return-value is 0 - The value that PostQuitMessage() gave */
- return messages.wParam;
- }
- /* This function is called by the Windows function DispatchMessage() */
- LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- int i_text1=1;
- int i_text2=2;
- int i_button1=3;
- int i_button2=4;
- int i_text3 = 5;
- int i_text4 = 6;
- int i_button3 = 7;
- int i_button4 = 8;
- char cleartext [1];
- cleartext [1] = '\0';
- switch (message) /* handle the messages */
- {
- case WM_DESTROY:
- PostQuitMessage (0); /* send a WM_QUIT to the message queue */
- break;
- case WM_CREATE: // after case WM_destroy
- // 1. Creating Text box
- text_box1= CreateWindow(TEXT("EDIT"), TEXT("Int"),WS_VISIBLE
- | WS_BORDER | WS_CHILD |ES_AUTOHSCROLL,
- 50,30,150,25,hwnd, (HMENU)i_text1, NULL,NULL);
- text_box2= CreateWindow(TEXT("EDIT"), TEXT("Binary "),WS_VISIBLE
- | WS_BORDER | WS_CHILD |ES_AUTOHSCROLL,
- 300,30,300,25,hwnd, (HMENU)i_text2, NULL,NULL);
- button1= CreateWindow(TEXT("BUTTON"), TEXT("To Binary"),WS_VISIBLE
- | WS_BORDER | WS_CHILD,
- 50,100,80,25,hwnd, (HMENU)i_button1, NULL,NULL);
- button2= CreateWindow(TEXT("BUTTON"), TEXT("Back to Int"),WS_VISIBLE
- | WS_BORDER | WS_CHILD,
- 300,100,80,25,hwnd, (HMENU)i_button2, NULL,NULL);
- text_box3= CreateWindow(TEXT("EDIT"), TEXT("Float"),WS_VISIBLE
- | WS_BORDER | WS_CHILD |ES_AUTOHSCROLL,
- 50,150,150,25,hwnd, (HMENU)i_text3, NULL,NULL);
- text_box4= CreateWindow(TEXT("EDIT"), TEXT("Binary "),WS_VISIBLE
- | WS_BORDER | WS_CHILD |ES_AUTOHSCROLL,
- 300,150,300,25,hwnd, (HMENU)i_text4, NULL,NULL);
- button3= CreateWindow(TEXT("BUTTON"), TEXT("To Binary"),WS_VISIBLE
- | WS_BORDER | WS_CHILD,
- 50,230,80,25,hwnd, (HMENU)i_button3, NULL,NULL);
- button4= CreateWindow(TEXT("BUTTON"), TEXT("Back to float"),WS_VISIBLE
- | WS_BORDER | WS_CHILD,
- 300,230,100,25,hwnd, (HMENU)i_button4, NULL,NULL);
- break;
- case WM_COMMAND:
- if (LOWORD(wParam)==i_button1)
- {MessageBox(hwnd,"int conversion","title",
- MB_OK|MB_ICONINFORMATION);
- char my_int_text [80];
- char buff[33];
- buff[32] = '\0';
- int bufsize = GetWindowTextLength(text_box1) ;
- int i=GetWindowText(text_box1,(char *)my_int_text,bufsize + 1) ;
- int my_int = atoi(my_int_text);
- cout << my_int << endl;
- convert_to_bin(my_int,buff);
- SetWindowText(text_box2, buff);
- SetWindowText(text_box1, cleartext);
- }//Button 1
- if (LOWORD(wParam)==i_button2)
- {
- char buff[33];
- buff[32] = '\0';
- int bufsize = GetWindowTextLength(text_box2);
- int i=GetWindowText(text_box2 ,(char *)buff,bufsize + 1) ;
- int my_int = back_to_int(buff);
- string int_text;
- int_text = to_string(my_int);
- SetWindowText(text_box1, int_text.c_str());
- }//Button 2
- if (LOWORD(wParam)==i_button3)
- {MessageBox(hwnd,"Float converstion","title",
- MB_OK|MB_ICONINFORMATION);
- char my_float_text [80];
- char buff[33];
- buff[32] = '\0';
- int bufsize = GetWindowTextLength(text_box3);
- int i=GetWindowText(text_box3,(char *)my_float_text,bufsize + 1) ;
- float my_float = atof(my_float_text);
- cout<<my_float<<endl;
- DisplayFloatRepresentation(my_float, buff);
- char buff1[33]={'/0'};
- for(int i = 0,j = 31 ; i <=31; i++, j--)
- {
- buff1[i] = buff[j];
- }
- buff1[32] = '\0';
- SetWindowText(text_box4, buff1);
- SetWindowText(text_box3, cleartext);
- }//Button 3
- if (LOWORD(wParam)==i_button4)
- {
- char buff[32];
- buff[32] = '\0';
- float bufsize = GetWindowTextLength(text_box4);
- int i=GetWindowText(text_box4 ,(char *)buff,bufsize + 1);
- char buff1[32];
- for(int i = 0,j = 31 ; i < 32; i++, j--)
- {
- buff1[i] = buff[j];
- }
- buff1[32] = '\0';
- cout<<"The float in Binary is : "<<buff1<<endl;
- float my_float = CharArrtoFloat(buff1);
- char float_text[10];
- string my_stringFloat=to_string(my_float);
- cout<<my_stringFloat<<endl;
- for(int i=0;i<=9;i++)
- {
- float_text[i]=my_stringFloat[i];
- }
- SetWindowText(text_box3, float_text);
- }//Button 4
- break;
- default:
- /* for messages that we don't deal with */
- return DefWindowProc (hwnd, message, wParam, lParam);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement