bincode.c

 #include <windows.h>
#include <string.h>
#include "bincoderes.h"

#define BUFFER_SIZE 10001

static BOOL CALLBACK DialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
static HINSTANCE hInst;

void Encode(HWND hwndDlg, char *text);
void Decode(HWND hwndDlg, char *text);

int APIENTRY WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
hInst=hinst;
memset(&wc,0,sizeof(wc));
wc.lpfnWndProc = DefDlgProc;
wc.cbWndExtra = DLGWINDOWEXTRA;
wc.hInstance = hinst;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszClassName = "bincode";
RegisterClassEx(&wc);

return DialogBox(hinst, MAKEINTRESOURCE(IDD_MAINDIALOG), NULL, (DLGPROC) DialogFunc);

}

void Encode(HWND hwndDlg, char *text){
char holder;
char newtext[BUFFER_SIZE];
int i, i2, f, x;
for(i = 1, i2=-1;i<=strlen(text);i++, i2+=8){
holder=text[i - 1];
for (x=128, f=1;x>=1;f+=1, x/=2){
if (holder>=x) {
newtext[i2 + f]='1';
newtext[i2 + f + 1]=0;
holder-=x;
}
else {
newtext[i2 + f]='0';
newtext[i2 + f + 1]=0;
}
}
}
SetDlgItemText(hwndDlg, displaybox, newtext);
}

void Decode(HWND hwndDlg, char *text){
char newtext[BUFFER_SIZE];
char holder2;
int i, x, f, i2, holder;
    if (strlen(text)<8) {
   SetDlgItemText(hwndDlg, displaybox, "Invalid binary.");
   return;
}
for (i2=1, i=1;i<=(strlen(text) / 8);i++, i2+=8){
holder=0;
for(x=128, f=1;f<=8;f++, x/=2){
holder2=text[(i2 - 2) + f];
if (((holder2!='1') && (holder2!='0')) || ((strlen(text) / 8) * 8!=strlen(text))) {
       SetDlgItemText(hwndDlg, displaybox, "Invalid binary.");
   return;
            }
if (holder2=='1'){
holder+=x;
}
}
newtext[i - 1]=holder;
newtext[i]=0;
}
SetDlgItemText(hwndDlg, displaybox, newtext);
}

static BOOL CALLBACK DialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
HICON hIcon, hIcons;
HANDLE tbox;
char intext[BUFFER_SIZE];
switch (msg) {
case WM_INITDIALOG:
hIcon=LoadImage(hInst, "binicon.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
hIcons=LoadImage(hInst, "binsmall.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
SendMessage(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
SendMessage(hwndDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcons);
break;
case WM_COMMAND:
switch (HIWORD(wParam)) {
    case EN_SETFOCUS:
if (LOWORD(wParam)==IDTEXTINPUT) {
    SetFocus(GetDlgItem(hwndDlg, IDTEXTINPUT));
           SendDlgItemMessage(hwndDlg, IDTEXTINPUT, EM_SETSEL, 0, -1);
    }
if (LOWORD(wParam)==displaybox) {
    SetFocus(GetDlgItem(hwndDlg, displaybox));
           SendDlgItemMessage(hwndDlg, displaybox, EM_SETSEL, 0, -1);
    }
}
switch (LOWORD(wParam)) {
case IDOK:
EndDialog(hwndDlg,1);
return 1;
case IDCANCEL:
EndDialog(hwndDlg,0);
return 1;
case IDENCODE:
GetDlgItemText(hwndDlg, IDTEXTINPUT, intext, BUFFER_SIZE);
Encode(hwndDlg, intext);
SetFocus(GetDlgItem(hwndDlg, IDTEXTINPUT));
SendDlgItemMessage(hwndDlg, IDTEXTINPUT, EM_SETSEL, 0, -1);
break;
case IDDECODE:
GetDlgItemText(hwndDlg, IDTEXTINPUT, intext, BUFFER_SIZE);
Decode(hwndDlg, intext);
SetFocus(GetDlgItem(hwndDlg, IDTEXTINPUT));
SendDlgItemMessage(hwndDlg, IDTEXTINPUT, EM_SETSEL, 0, -1);
break;
case IDCOPY:
SetFocus(GetDlgItem(hwndDlg, displaybox));
SendDlgItemMessage(hwndDlg, displaybox, EM_SETSEL, 0, -1);
SendDlgItemMessage(hwndDlg, displaybox, WM_COPY, 0, 0);
SetFocus(GetDlgItem(hwndDlg, IDTEXTINPUT));
SendDlgItemMessage(hwndDlg, IDTEXTINPUT, EM_SETSEL, 0, -1);
      break;
}
break;
case WM_CLOSE:
EndDialog(hwndDlg,0);
return TRUE;

}
return FALSE;
}


Project Homepage: