Marc
Nombre de messages : 2466 Age : 63 Localisation : TOURS (37) Date d'inscription : 17/03/2014
| Sujet: Convertisseur binaire vers décimal / Hexadécimal Dim 26 Nov 2017 - 12:38 | |
| Bonjour à tous ! Voici un convertisseur binaire vers décimal/hexadécimal organisé en 4 octets (4 x 8 bits), utilisable en 2 words (2 x 16 bits) ou en Dword (1 x 32 bit). A l’aide des 32 boutons numérotés de 0 à 31, on peut activer n’importe quel bit et obtenir la valeur décimale/hexadécimale correspondante. ATTENTION : il est conçu pour convertir des entiers non signés !Parfois plus simple que l’utilisation d’une calculatrice où il faut à chaque fois retaper l’ensemble de la valeur binaire pour avoir le résultat, cet utilitaire peut s’avérer utile, surtout en électronique. Bonne journée à tous ! - Code:
-
' ------------------------------------------------------------------------------ ' PANORAMIC BINARY TO DECIMAL / HEXADECIMAL FOR UNSIGNED INTEGER ' MARC - November 2017 - Panoramic v0.9.28.i12 ' http://panoramic-language.pagesperso-orange.fr/French/index.html ' http://panoramic.top-forum.net ' ------------------------------------------------------------------------------ WIDTH 0, 900 HEIGHT 0, 400 LEFT 0,(screen_x-900)/2 TOP 0,(screen_y-400)/2 LABEL BitState, Reset DIM i%, bit%, x, Poids% DIM Result$, Octet1$, Octet2$, Octet3$, Octet4$, Word1$, Word2$ DIM Hexa1$, Hexa2$, Hexa3$, Hexa4$ DIM StateButton%(32) FONT_NAME 0,"Arial" FONT_SIZE 0, 12 FOR i% = 1 TO 8 BUTTON i% LEFT i%, 25*i% TOP i%, 110 WIDTH i%, 25 HEIGHT i%, 25 ALPHA i%+100 LEFT i%+100, (25*i%)+4 TOP i%+100, 85 WIDTH i%+100, 25 HEIGHT i%+100, 25 CAPTION i%+100,32-i% NEXT i% FOR i% = 9 to 16 BUTTON i% LEFT i%, 10+(25*i%) TOP i%, 110 WIDTH i%, 25 HEIGHT i%, 25 ALPHA i%+100 LEFT i%+100, (25*i%)+14 TOP i%+100, 85 WIDTH i%+100, 25 HEIGHT i%+100, 25 CAPTION i%+100,32-i% NEXT i% FOR i% = 17 TO 24 BUTTON i% LEFT i%, 20+(25*i%) TOP i%, 110 WIDTH i%, 25 HEIGHT i%, 25 ALPHA i%+100 LEFT i%+100, (25*i%)+24 IF i% = 24 THEN LEFT i%+100, (25*i%)+28 IF i% = 23 THEN LEFT i%+100, (25*i%)+28 TOP i%+100, 85 WIDTH i%+100, 25 HEIGHT i%+100, 25 CAPTION i%+100,32-i% NEXT i% FOR i% = 25 to 32 BUTTON i% LEFT i%, 30+(25*i%) TOP i%, 110 WIDTH i%, 25 HEIGHT i%, 25 ALPHA i%+100 LEFT i%+100, (25*i%)+38 TOP i%+100, 85 WIDTH i%+100, 25 HEIGHT i%+100, 25 CAPTION i%+100,32-i% NEXT i% FOR i% = 1 to 32 CAPTION i% , "0" StateButton%(i%) = 0 NEXT i% ALPHA 33 TOP 33,35 LEFT 33,125 FONT_SIZE 33,16 CAPTION 33,"BINARY TO DECIMAL / HEXADECIMAL FOR UNSIGNED INTEGER" ALPHA 34 TOP 34, 250 LEFT 34, 250 FONT_SIZE 34, 16 ALPHA 35 TOP 35, 150 LEFT 35, 250 FONT_SIZE 35, 16 ALPHA 36 TOP 36, 150 LEFT 36, 250 FONT_SIZE 36, 16 ALPHA 37 TOP 37, 150 LEFT 37, 250 FONT_SIZE 37, 16 ALPHA 38 TOP 38, 150 LEFT 38, 250 FONT_SIZE 38, 16 ALPHA 39 TOP 39, 200 LEFT 39, 250 FONT_SIZE 39, 16 ALPHA 40 TOP 40, 200 LEFT 40, 250 FONT_SIZE 40, 16 FOR i% = 41 to 44 BUTTON i% WIDTH i%, 100 TOP i%,300 LEFT i%,210*i%-8523 CAPTION i%, "Reset "+STR$(45-i%) ON_CLICK i%,Reset NEXT i% FOR i% = 1 to 32 ON_CLICK i%, BitState NEXT i% LINES() END ' ------------------------------------------------------------------------------ BitState: i%=NUMBER_CLICK IF StateButton%(i%) = 0 StateButton%(i%) = 1 Caption i%, "1" ELSE StateButton%(i%) = 0 CAPTION i%,"0" END_IF Lines() Octet1Display() Octet2Display() Octet3Display() Octet4Display() Word1Display() Word2Display() DwordDisplay() RETURN ' ------------------------------------------------------------------------------ SUB Octet4Display() DIM_LOCAL a$,j% x = 0 FOR Bit% = 8 TO 1 STEP -1 IF StateButton%(Bit%) = 1 THEN x = x + POWER(2,8-Bit%) NEXT Bit% Hexa4$=HEX$(x) IF LEN(Hexa4$)=1 THEN Hexa4$="0"+Hexa4$ a$=STR$(x)+" - 0x"+Hexa4$ j%=TEXT_WIDTH(a$,38) LEFT 38, ((885-j%)/2)-317 CAPTION 38, a$ END_SUB ' ------------------------------------------------------------------------------ SUB Octet3Display() DIM_LOCAL a$,j% x = 0 FOR Bit% = 16 TO 9 STEP -1 IF StateButton%(Bit%) = 1 THEN x = x + POWER(2,16-Bit%) NEXT Bit% Hexa3$=HEX$(x) IF LEN(Hexa3$)=1 THEN Hexa3$="0"+Hexa3$ a$=STR$(x)+" - 0x"+Hexa3$ j%=TEXT_WIDTH(a$,37) LEFT 37, ((885-j%)/2)-107 CAPTION 37, a$ END_SUB ' ------------------------------------------------------------------------------ SUB Octet2Display() DIM_LOCAL a$,j% x = 0 FOR Bit% = 24 TO 17 STEP -1 IF StateButton%(Bit%) = 1 THEN x = x + POWER(2,24-Bit%) NEXT Bit% Hexa2$=HEX$(x) IF LEN(Hexa2$)=1 THEN Hexa2$="0"+Hexa2$ a$=STR$(x)+" - 0x"+Hexa2$ j%=TEXT_WIDTH(a$,36) LEFT 36, ((885-j%)/2)+103 CAPTION 36, a$ END_SUB ' ------------------------------------------------------------------------------ SUB Octet1Display() DIM_LOCAL a$,j% x = 0 FOR Bit% = 32 TO 25 STEP -1 IF StateButton%(Bit%) = 1 THEN x = x + POWER(2,32-Bit%) NEXT Bit% Hexa1$=HEX$(x) IF LEN(Hexa1$)=1 THEN Hexa1$="0"+Hexa1$ a$=STR$(x)+" - 0x"+Hexa1$ j%=TEXT_WIDTH(a$,35) LEFT 35, ((885-j%)/2)+313 CAPTION 35, a$ END_SUB ' ------------------------------------------------------------------------------ SUB Word1Display() DIM_LOCAL a$,j% x = 0 FOR Bit% = 32 TO 17 STEP -1 IF StateButton%(Bit%) = 1 THEN x = x + POWER(2,32-Bit%) NEXT Bit% a$=STR$(x)+" - 0x"+Hexa2$+Hexa1$ j%=TEXT_WIDTH(a$,39) LEFT 39, ((885-j%)/2)+207 CAPTION 39, a$ END_SUB ' ------------------------------------------------------------------------------ SUB Word2Display() DIM_LOCAL a$,j% x = 0 FOR Bit% = 16 TO 1 STEP -1 IF StateButton%(Bit%) = 1 THEN x = x + POWER(2,16-Bit%) NEXT Bit% a$=STR$(x)+" - 0x"+Hexa4$+Hexa3$ j%=TEXT_WIDTH(a$,40) LEFT 40, ((885-j%)/2)-210 CAPTION 40, a$ END_SUB ' ------------------------------------------------------------------------------ SUB DwordDisplay() DIM_LOCAL j%, k%, a$, b$ x = 0 FOR Bit% = 32 TO 1 STEP -1 IF StateButton%(Bit%) = 1 THEN x = x + POWER(2,32-Bit%) NEXT Bit% Result$="" k%=0 a$=STR$(x) j%=LEN(a$) FOR i%= j% TO 1 STEP -1 k%=k%+1 b$=MID$(a$,i%,1) Result$=b$+Result$ IF k%=3 AND i% > 1 Result$=chr$(46)+Result$ K%=0 END_IF NEXT i% Result$=Result$+" - 0x"+Hexa4$+Hexa3$+" "+Hexa2$+Hexa1$ j%=TEXT_WIDTH(Result$,34) LEFT 34, (885-j%)/2 CAPTION 34, Result$ END_SUB ' ------------------------------------------------------------------------------ SUB Lines() 2D_LINE 30,174,221,174 2D_LINE 240,174,431,174 2D_LINE 450,174,641,174 2D_LINE 660,174,851,174 DISPLAY 2D_LINE 30,224,433,224 2D_LINE 448,224,853,224 2D_LINE 30,274,853,274 2D_LINE 30,159,30,174 DISPLAY 2D_LINE 220,159,220,174 2D_LINE 240,159,240,174 2D_LINE 430,159,430,174 2D_LINE 450,159,450,174 DISPLAY 2D_LINE 640,159,640,174 2D_LINE 660,159,660,174 2D_LINE 850,159,850,174 2D_LINE 30,209,30,224 DISPLAY 2D_LINE 432,209,432,224 2D_LINE 448,209,448,224 2D_LINE 852,209,852,224 2D_LINE 30,259,30,274 2D_LINE 852,259,852,274 DISPLAY END_SUB ' ------------------------------------------------------------------------------ Reset: i% = NUMBER_CLICK SELECT i% CASE 41 FOR Bit%=1 to 8 StateButton%(Bit%)=0 CAPTION Bit%, 0 NEXT Bit% CASE 42 FOR Bit%=9 to 16 StateButton%(Bit%)=0 CAPTION Bit%, 0 NEXT Bit% CASE 43 FOR Bit%=17 to 24 StateButton%(Bit%)=0 CAPTION Bit%, 0 NEXT Bit% CASE 44 FOR Bit%=25 to 32 StateButton%(Bit%)=0 CAPTION Bit%, 0 NEXT Bit% END_SELECT Lines() Octet1Display() Octet2Display() Octet3Display() Octet4Display() Word1Display() Word2Display() DwordDisplay() RETURN EDIT : ajout de la notation hexadécimale. 2ème EDIT : modification de la numérotation des Bits : de 0 à 31
Dernière édition par Marc le Mar 28 Nov 2017 - 21:19, édité 10 fois | |
|