Ayant récemment eu besoin de convertir du texte en binaire et inversement, j'ai réalisé ce petit programme. Pour le décodage, il faut obligatoirement un espace tous les 8 caractères ou pas du tout d'espace, sinon, ça bugue un peu. C'est pas super commenté, j'ai pioché des codes par-ci par-là, mais au moins ça marche bien :
► Afficher le texteBinaryConverter
Code : Tout sélectionner
;Merci à Ptrex pour les fonctions de conversions hexadécimales.
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=BinaryConverter.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $Form1 = GUICreate("BinaryConverter par RL77LUC", 529, 218, 192, 124)
Global $Edit1 = GUICtrlCreateEdit("", 8, 24, 249, 153, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN))
Global $Edit2 = GUICtrlCreateEdit("", 272, 24, 249, 153, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN))
Global $Label1 = GUICtrlCreateLabel("Texte", 8, 6, 247, 17, $SS_CENTER)
Global $Label2 = GUICtrlCreateLabel("Binaire", 272, 6, 244, 17, $SS_CENTER)
Global $Button1 = GUICtrlCreateButton("Convertir en binaire >>>", 8, 184, 249, 25, $WS_GROUP)
Global $Button2 = GUICtrlCreateButton("<<< Convertir en texte", 272, 184, 249, 25, $WS_GROUP)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
$texte = GuiCtrlRead($Edit1)
If $texte <> "" Then
$hexa = StringToBinary($texte)
$bin_long = HexaToBinary($hexa)
$bin = StringMid($bin_long,9)
Global $bin_coup[StringLen($bin)]
For $i = 1 to StringLen($bin) Step 8
$bin_coup[$i] = StringMid($bin,$i,8)
Next
Global $bin_output[UBound($bin_coup)]
$togglable = 0
For $i = 1 to UBound($bin_coup) Step 8
$togglable = $togglable + 1
If $togglable = 5 Then
$bin_output = StringFormat('%s' & @CRLF & '%s',$bin_output, $bin_coup[$i])
$togglable = 1
Else
$bin_output = StringFormat('%s %s',$bin_output, $bin_coup[$i])
EndIf
Next
GuiCtrlSetData($Edit2,StringMid($bin_output,2))
Else
GuiCtrlSetData($Edit2,'')
EndIf
GuiCtrlSetData($Edit1,"")
Case $Button2
$bin = GuiCtrlRead($Edit2)
If $bin <> "" Then
$bin_bisb = StringReplace($bin," ","")
$bin_bis = StringReplace($bin_bisb,@CRLF,"")
$hexa = BinaryToHexa($bin_bis)
$texte = BinaryToString("0x"&$hexa)
GuiCtrlSetData($Edit1,$texte)
Else
GuiCtrlSetData($Edit1,'')
EndIf
GuiCtrlSetData($Edit2,"")
EndSwitch
WEnd
Func HexaToBinary($hexa)
$hexvalue = StringSplit($hexa,'')
$Result = ''
$bits = "0000|0001|0010|0011|0100|0101|0110|0111|1000|1001|1010|1011|1100|1101|1110|1111"
$bits = stringsplit($bits,'|')
for $n = 1 to $hexvalue[0]
$result &= $bits[Dec($hexvalue[$n])+1]
Next
Return $result
EndFunc
Func BinaryToHexa($BinaryValue)
Local $test, $Result = '',$numbytes,$nb
If StringRegExp($BinaryValue,'[0-1]') then
if $BinaryValue = '' Then
SetError(-2)
Return
endif
Local $bits = "0000|0001|0010|0011|0100|0101|0110|0111|1000|1001|1010|1011|1100|1101|1110|1111"
$bits = stringsplit($bits,'|')
#region check string is binary
$test = stringreplace($BinaryValue,'1','')
$test = stringreplace($test,'0','')
if $test <> '' Then
SetError(-1);non binary character detected
Return
endif
#endregion check string is binary
#region make binary string an integral multiple of 4 characters
While 1
$nb = Mod(StringLen($BinaryValue),4)
if $nb = 0 then exitloop
$BinaryValue = '0' & $BinaryValue
WEnd
#endregion make binary string an integral multiple of 4 characters
$numbytes = Int(StringLen($BinaryValue)/4);the number of bytes
Dim $bytes[$numbytes],$Deci[$numbytes]
For $j = 0 to $numbytes - 1;for each byte
;extract the next byte
$bytes[$j] = StringMid($BinaryValue,1+4*$j,4)
;find what the dec value of the byte is
for $k = 0 to 15;for all the 16 possible hex values
if $bytes[$j] = $bits[$k+1] Then
$Deci[$j] = $k
ExitLoop
EndIf
next
Next
;now we have the decimal value for each byte, so stitch the string together again
$Result = ''
for $l = 0 to $numbytes - 1
$Result &= Hex($Deci[$l],1)
Next
return $Result
EndIf
EndFunc