Convertitore decimale-esadecimale

« Older   Newer »
  Share  
Sbarabau
CAT_IMG Posted on 5/7/2010, 10:17     +1   -1




Piccolo programma che converte i numeri decimali in esadecimali e viceversa.
L'ho creato principalmente per mantenermi in allenamento, ma soprattutto perchè mi sto avvicinando (seppur lentamente) al mondo dell'assembly, e mi serviva un tool per convertire da decimale a esadecimale, quindi... perchè non crearlo? ^^

**Funzione per il decrypt fatta interamente da me**

L'UDF MsgBox si può trovare nell'apposita sezione di questo forum

SPOILER (click to view)
CODICE
#include <GuiConstants.au3>
#include <ButtonConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBox.au3>
#include <String.au3>
#NoTrayIcon

$Gui = GUICreate ("Convertitore decimale-esadecimale", 350, 132)
    GUISetState (@SW_SHOW)

GUICtrlCreateLabel ("Convertitore decimale-esadecimale", 5, 5, 300, 35)
        GUICtrlSetFont(-1, 13, 600, 2, "Gill Sans Ultra Bold Condensed")
        GUICtrlSetColor(-1, 0x000000)
        GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)

$Decimale = GUICtrlCreateInput ("", 5, 35, 275, 22, 0x2000)
$Esadecimale = GUICtrlCreateInput ("", 5, 62, 275, 22)
$Bottone_Decimale = GUICtrlCreateButton ("Crypt", 285, 35, 60, 22)
$Bottone_Esadecimale = GUICtrlCreateButton ("Decrypt", 285, 62, 60, 22)
$Istruzioni = GUICtrlCreateLabel ("Inserire un numero da criptare o un codice esadecimale da decriptare", 7, 95, 345, 20)
GUISetState (@SW_SHOW)

While 1=1
Switch GUIGetMsg ()
  Case $Bottone_Decimale
        $Leggi_Decimale = Number (GUICtrlRead ($Decimale))
        MsgBox (0, "", $Leggi_Decimale)
          If $Leggi_Decimale = 0 Then
                GUICtrlSetData ($Esadecimale, 0)
          ElseIf $Leggi_Decimale = "" Then
                MsgBox ($Avviso_Ok, "Attenzione", "Inserire un numero da criptare!")
          Else
                GUICtrlSetData ($Istruzioni, "Criptaggio in corso...")
                Sleep (250)
                $Hex = Hex ($Leggi_Decimale)
                  If StringLen ($Hex) > 1 Then
                          For $i = 1 To StringLen ($Hex)
                                  If StringLeft ($Hex, 1) = "0" Then
                                        $Hex = StringRight ($Hex, 8 - $i)
                                  Else
                                        ExitLoop
                                  EndIf
                          Next
                  EndIf
                GUICtrlSetData ($Esadecimale, $Hex)
                GUICtrlSetData ($Istruzioni, "Criptaggio completato")
                Sleep (500)
                GUICtrlSetData ($Istruzioni, "Inserire un numero da criptare o un codice binario da decriptare")
          EndIf
 
  Case $Bottone_Esadecimale
        $Leggi_Esadecimale = GUICtrlRead ($Esadecimale)
          If $Leggi_Esadecimale = "" Then
                MsgBox ($Avviso_Ok, "Attenzione", "Inserire un codice binario da decriptare!")
          ElseIf StringIsXDigit ($Leggi_Esadecimale) = 0 Then
                MsgBox ($Avviso_Ok, "Attenzione", "Inserire un codice esadecimale valido!")
          Else
                GUICtrlSetData ($Istruzioni, "Decriptaggio in corso...")
                Sleep (250)
                GUICtrlSetData ($Decimale, _Base16_Decrypt ($Leggi_Esadecimale))
                GUICtrlSetData ($Istruzioni, "Decriptaggio completato")
                Sleep (500)
                GUICtrlSetData ($Istruzioni, "Inserire un numero da criptare o un codice binario da decriptare")
          EndIf
 
  Case $GUI_EVENT_CLOSE
        Exit
EndSwitch
WEnd

Func _Base16_Decrypt ($String)
Local $Risultato = 0
Local $Base_10[16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Local $Base_16[16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"]
Local $Lettera_Attuale = 0
 
  $String = StringReplace ($String, "x", "")
  If Not StringLen ($String) = 1 Then
          For $i = 1 To 8
                  If StringLeft ($String, 1) = 0 Then
                        $String = StringTrimLeft ($String, StringLen ($String) - 1)
                  Else
                        ExitLoop
                  EndIf
          Next
  EndIf
       
$String = _StringReverse ($String)
  For $i = 1 To StringLen ($String)
        $Lettera_Attuale = StringMid ($String, $i, 1)
          For $i2 = 1 To UBound ($Base_16) - 1
                If $Base_16[$i2] = $Lettera_Attuale Then $Lettera_Attuale = $Base_10[$i2]
          Next
          If $Risultato = 0 Then
                $Risultato += $Lettera_Attuale * (16 ^ (Number ($i) - 1))
          Else
                $Risultato += $Lettera_Attuale * (16 ^ (Number ($i) - 1))
          EndIf
  Next

Return $Risultato
EndFunc


Edited by Sbarabau - 5/7/2010, 21:59

Download attachment
Convertitore_decimale_esadecimale.zip ( Number of downloads: 410 )

 
Top
ACIDBURN16
CAT_IMG Posted on 5/7/2010, 11:20     +1   -1




Bello :)
 
Top
Sbarabau
CAT_IMG Posted on 5/7/2010, 20:58     +1   -1




Grazie ^^

**Corretto un bug che impediva, in alcuni casi, una corretta conversione dalla base esadecimale a quella decimale**
 
Top
Gianlu-thebest
CAT_IMG Posted on 6/7/2010, 00:21     +1   -1




MOOOlto bello!
 
Top
3 replies since 5/7/2010, 10:17   1442 views
  Share