|
Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
CONVERSION DEC / HEX / BIN (WIN 32)
Information sur la source
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
- #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;
- }
#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.
Fichier Zip
Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !
Télécharger le zip
Historique
- 13 août 2004 22:14:58 :
- nouvelle version
- 13 août 2004 22:19:14 :
- petit oubli
- 15 octobre 2005 18:43:24 :
- prog refais, gere les conversions de nombres sur 64 bits.
- 09 mai 2007 15:40:26 :
- correction bug
Sources du même auteur
Sources de la même categorie
Sources en rapport avec celle ci
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
conversion binaire-décimal en C [ par Tooshort23 ]
Bonjour,je suis étudiant en IUT GEII.Savais vous convertir un nombre binaire (de taille: 32 bits) en décimal ?merci de me donner des conseils.Si possi
Bis: Binaire en C [ par fredleconte ]
Réponse acceptée ! Mon problème n'est pas de convertir un nombre d'un base a l'autre mais de l'écrire comme en basic "%1010101" ou "%" désigne un nomb
conversion d'une chaine type "01001000" en binaire [ par brennyboy ]
Bonjour à tous !J'aimerais savoir comment passer d'une chaine de 8 caracteres contenant seulement de 0 et des 1, au chiffre décimal correspo
fonctions de base d'un arbre binaire [ par abdelkaderg54 ]
Salut tout le monde ..Ben alors j'ai un probleme avec les arbre binaires ,je suis a la recherche des fonctions des base suivantes en c++:1/declaration
conversion ip [ par mrtatou ]
bonjour,voila je galere dur en c (je suis un novice!!!), je voudrai convertir une adresse ip en décimal; c'est à dire j'ai mon adresse du ty
algo conversion fiichier en hexadécimal [ par developvbdebut ]
BonsoirJe cherche un algo pour convertir des fichiers en Hexadécimal.J'ai cherché sur le web, mai je n'ai pas trouvé.Pouvez vous m'aider?Merci.Bonne A
conversion des données EXCEL vers une base de données ACCESS [ par mallouka ]
salut,j'ai besoin d'un logiciel ou un code source permettant de convertir une page XLS afin d'alimenter unedatabase ACCESS.s'il vous plaît ,j'ai
CRC + affichage/conversion binaire [ par Ferrari01 ]
Bonjour...Est-ce que quelqu'un aurait un petit programme de CRC et/ou un petit programme qui permet d'afficher 4 caractères en binaire (ex.: abcd ->
probleme de conversion binaire [ par montie_s ]
Je cree un programme capable de transformer les codes ascii de caracteres en leur equivalence en base 2, puis de reprendre ces nombre en binaire pour
Aide Conversion !!! [ par vincfred ]
slt jai un programme a faire pour mon licé et je comprend rien le prof ma doné ke le fichier de déclaration jespere ke kelkun pouré maider voici le fi
|
Téléchargements
Logiciels à télécharger sur le même thème :
|