Conversion dec / hex / bin (win 32)

Description

Bon, je sais qu'il y a deja pas mal de sources sur ces conversions, j'aporte juste une "interface graphique" avec ce prog, si on peut appeller ca comme ca.

Source / Exemple :


#include <windows.h>
#include "resource.h"

//----------------------------------------------------------------------------------
// variables globales
//----------------------------------------------------------------------------------
static HWND hDec;
static HWND hHex;
static HWND hBin;
static WNDPROC pOldEditProc;

// char to int table
static char c2i_table[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
                           0,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35, 0, 0, 0, 0, 0,
                           0,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35, 0, 0, 0, 0, 0,
                           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

// int to char table
static char i2c_table[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

//----------------------------------------------------------------------------------
// base
// - inbase : entre 2 et 16
// - outbase : entre 2 et 16
//----------------------------------------------------------------------------------
char* base(char* number, int inbase, int outbase)
{
	char* p = NULL;
   char* b = NULL;
	unsigned __int64 nb = 0;

	static char buff[128];

	// évalue number
	for(p = number; *p != '\0'; p++)
	{
		nb *= inbase;
      nb += c2i_table[*((unsigned char*)p)];
	}

	// écrit le nombre convertis dans buff
	for(p = buff; nb > 0; p++)
	{

  • p = i2c_table[nb % outbase];
nb /= outbase; } // retourne la chaine for(b = buff, *p-- = '\0'; b < p; b++, p--) { char tmp = *b;
  • b = *p;
  • p = tmp;
} return buff; } //---------------------------------------------------------------------------------- // UpdateContent //---------------------------------------------------------------------------------- void UpdateContent(HWND hEdit, int inbase) { char buff[128]; GetWindowText(hEdit, buff, 128); switch(inbase) { case 2: SetWindowText(hDec, base(buff, 2, 10)); SetWindowText(hHex, base(buff, 2, 16)); break; case 10: SetWindowText(hBin, base(buff, 10, 2)); SetWindowText(hHex, base(buff, 10, 16)); break; case 16: SetWindowText(hBin, base(buff, 16, 2)); SetWindowText(hDec, base(buff, 16, 10)); break; } } //---------------------------------------------------------------------------------- // EditProc //---------------------------------------------------------------------------------- LRESULT CALLBACK EditProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if(uMsg == WM_CHAR) { WPARAM key = wParam; // autorise copier/coller + effacer if(key == 0x03 || key == 0x16 || key == VK_BACK) goto defproc; // n'autorise que certains caracteres if(hWnd == hDec) // 01234567890 { if(key >= '0' && key <= '9') goto defproc; return 0; } else if(hWnd == hHex) // 0123456789 abcdef ABCDEF { if((key>= '0' && key <= '9') || (key >= 'a' && key <= 'f') || (key >= 'A' && key <= 'F')) goto defproc; return 0; } else if(hWnd == hBin) // 01 { if(key != '0' && key != '1') return 0; } } defproc: return CallWindowProc(pOldEditProc, hWnd, uMsg, wParam, lParam); } //---------------------------------------------------------------------------------- // AppDlgProc //---------------------------------------------------------------------------------- INT_PTR CALLBACK AppDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_INITDIALOG: { // editbox hDec = GetDlgItem(hDlg, IDC_DEC); hHex = GetDlgItem(hDlg, IDC_HEX); hBin = GetDlgItem(hDlg, IDC_BIN); SendMessage(hDec, EM_SETLIMITTEXT, 20, 0); SendMessage(hHex, EM_SETLIMITTEXT, 16, 0); SendMessage(hBin, EM_SETLIMITTEXT, 64, 0); // sous classement pOldEditProc = (WNDPROC)(LONG_PTR) SetWindowLong(hDec, GWL_WNDPROC,(LONG)(LONG_PTR) EditProc); SetWindowLong(hHex, GWL_WNDPROC,(LONG)(LONG_PTR) EditProc); SetWindowLong(hBin, GWL_WNDPROC,(LONG)(LONG_PTR) EditProc); // focus wParam = (WPARAM) hBin; } return TRUE; case WM_COMMAND: switch(wParam) { // update texte des edits case MAKEWPARAM(IDC_DEC, EN_CHANGE): if(hDec == GetFocus()) UpdateContent(hDec, 10); return FALSE; case MAKEWPARAM(IDC_HEX, EN_CHANGE): if(hHex == GetFocus()) UpdateContent(hHex, 16); return FALSE; case MAKEWPARAM(IDC_BIN, EN_CHANGE): if(hBin == GetFocus()) UpdateContent(hBin, 2); return FALSE; case IDCANCEL: EndDialog(hDlg, 0); return FALSE; } } return FALSE; } //---------------------------------------------------------------------------------- // WinMain //---------------------------------------------------------------------------------- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_MAINDIALOG), NULL, AppDlgProc, 0); return 0; }

Conclusion :


L'executable est dans le repertoire release, il faut renomer le fichier .ex_ en .exe.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.