ptrex

Converter - Dec - Hex - Bin

17 posts in this topic

#1 ·  Posted (edited)

Converter - Dec - Hex - Bin

For all of those who need to convert Dec - Hex - Bin and visa versa.

#include <Color.au3>
#include <GUIConstantsEX.au3>
#include <String.au3>

Global Const $HX_REF="0123456789ABCDEF"
Global Const $color1 = 0x99A8AC
Global Const $color2 = 0xFFFFFF

$Gui = GUICreate("Converter @ Dec - Bin - Hex"& _StringRepeat(" ",125) & " by ptrex" , 833, 247, 161, 166)
GUISetIcon (@windowsdir & "system32Calc.exe",-1,$Gui)
$Size = WinGetClientSize($Gui)

$radio1 = GUICtrlCreateRadio ("Dec", 100, 10, 60, 20)
GUICtrlSetBkColor(-1,0xA1AFB2)
$radio2 = GUICtrlCreateRadio ("Hex", 160, 10, 60, 20)
GUICtrlSetBkColor(-1,0xA1AFB2)
$radio3 = GUICtrlCreateRadio ("Bin", 220, 10, 60, 20)
GUICtrlSetBkColor(-1,0xA1AFB2)
;GUICtrlSetState ($radio1, $GUI_CHECKED)

$label = GUICtrlCreateLabel("Input", 10, 15, 50, 15)
GUICtrlSetBkColor(-1,0xA1AFB2)
$labelout = GUICtrlCreateLabel("Result", 10, 45, 50, 15)
GUICtrlSetBkColor(-1,0xAEBABD)
$Input1 = GUICtrlCreateInput("", 100, 40, 650, 21)
$Label1 = GUICtrlCreateLabel("Hexadecimal", 10, 80, 70, 15)
GUICtrlSetBkColor(-1,0xBAC4C7)
$Label1out = GUICtrlCreateLabel("0", 100, 80, 600, 15)
GUICtrlSetBkColor(-1,0xBAC4C7)
$Label2 = GUICtrlCreateLabel("Binary", 10, 115, 50, 15)
GUICtrlSetBkColor(-1,0xCBD3D5)
$Label2out = GUICtrlCreateLabel("0", 100, 115, 600, 15)
GUICtrlSetBkColor(-1,0xCBD3D5)

_GUICtrlCreateGradient($color1, $color2, 0, 0, $size[0]*1.5, $size[1])
GUISetState(@SW_SHOW)


While 1
$msg = GUIGetMsg()
Select
        Case $msg = $GUI_EVENT_CLOSE
ExitLoop
Case $msg = $radio1 And BitAND(GUICtrlRead($radio1), $GUI_CHECKED) = $GUI_CHECKED

            GUICtrlSetData($Label1out,_DecimalToHex(int(StringStripWS(GUICtrlRead($Input1,1),8))))
            GUICtrlSetData($Label1,"Hexadecimal")
            GUICtrlSetData($Label2out,_DecToBinary(StringStripWS(GUICtrlRead($Input1,1),8)))
            GUICtrlSetData($Label2,"Binary")
                ;ConsoleWrite("_DecimalToHex " & _DecimalToHex(Int(GUICtrlRead($Input1,1))) & @LF)
                ;ConsoleWrite("_DecToBinary " & _DecToBinary(GUICtrlRead($Input1,1)) & @CRLF)
                                
        Case $msg = $radio2 And BitAND(GUICtrlRead($radio2), $GUI_CHECKED) = $GUI_CHECKED
GUICtrlSetData($Label1out,_HexToDecimal(StringStripWS(GUICtrlRead($Input1,1),8)))
            GUICtrlSetData($Label1,"Decimal")
            GUICtrlSetData($Label2out,_HexToBinaryString(StringStripWS(GUICtrlRead($Input1,1),8)))
            GUICtrlSetData($Label2,"Binary")
                ;ConsoleWrite("_HexToDecimal " & _HexToDecimal(GUICtrlRead($Input1,1)) & @LF)
                ;ConsoleWrite("_HexToBinaryString " & _HexToBinaryString(GUICtrlRead($Input1,1)) & @LF)
                
        Case $msg = $radio3 And BitAND(GUICtrlRead($radio3), $GUI_CHECKED) = $GUI_CHECKED
GUICtrlSetData($Label1out,_BinaryToHexString(StringStripWS(GUICtrlRead($Input1,1),8)))
            GUICtrlSetData($Label1,"Hexadecimal")
            GUICtrlSetData($Label2out,_BinaryToDec(StringStripWS(GUICtrlRead($Input1,1),8)))
            GUICtrlSetData($Label2,"Decimal")
                ;ConsoleWrite("_BinaryToHexString " & _BinaryToDec(GUICtrlRead($Input1,1)) & @LF)
                ;ConsoleWrite("_HexToBinaryString " & _HexToBinaryString(GUICtrlRead($Input1,1)) & @LF)     
EndSelect
WEnd

; Conversion Code - Chart
; DECIMAL 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
; HEX 0 1 2 3 4 5 6 7 8 9 A B C D E F 10
; BINARY 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 10000


; --------------------- Functions -----------------------------
; Binary to Decimal
Func _BinaryToDec($strBin)
Local $Return
Local $lngResult
Local $intIndex

If StringRegExp($strBin,'[0-1]') then
$lngResult = 0
For $intIndex = StringLen($strBin) to 1 step -1
$strDigit = StringMid($strBin, $intIndex, 1)
Select
case $strDigit="0"
; do nothing
case $strDigit="1"
$lngResult = $lngResult + (2 ^ (StringLen($strBin)-$intIndex))
case else
; invalid binary digit, so the whole thing is invalid
$lngResult = 0
$intIndex = 0 ; stop the loop
EndSelect
Next

$Return = $lngResult
    Return $Return
Else
    MsgBox(0,"Error","Wrong input, try again ...")
    Return
EndIf   
EndFunc

; Decimal To Binary
Func _DecToBinary($iDec)
Local $i, $sBinChar = ""

If StringRegExp($iDec,'[[:digit:]]') then
$i = 1

Do
$x = 16^$i
$i +=1
; Determine the Octets
Until $iDec < $x

For $n = 4*($i-1) To 1 Step -1
If BitAND(2 ^ ($n-1), $iDec) Then
$sBinChar &= "1"
Else
$sBinChar &= "0"
EndIf
Next
Return $sBinChar
Else
    MsgBox(0,"Error","Wrong input, try again ...")
    Return
EndIf   
EndFunc


; #FUNCTION# ==============================================================
; Function Name..: _HexToDec ( "expression" )
; Description ...: Returns decimal expression of a hexadecimal string.
; Parameters ....: expression - String representation of a hexadecimal expression to be converted to decimal.
; Return values .: Success - Returns decimal expression of a hexadecimal string.
; Failure - Returns "" (blank string) and sets @error to 1 if string is not hexadecimal type.
; Author ........: jennico (jennicoattminusonlinedotde)
; Remarks .......: working input format: "FFFF" or "0xFFFF" (string format), do NOT pass 0xFFFF without quotation marks (number format).
; current AutoIt Dec() limitation: 0x7FFFFFFF (2147483647).
; Related .......: Hex(), Dec(), _DecToHex()
; =======================================================================
Func _HexToDecimal($hx_hex)
If StringLeft($hx_hex, 2) = "0x" Then $hx_hex = StringMid($hx_hex, 3)
If StringIsXDigit($hx_hex) = 0 Then
SetError(1)
        MsgBox(0,"Error","Wrong input, try again ...")
Return ""
EndIf
Local $ret="", $hx_count=0, $hx_array = StringSplit($hx_hex, ""), $Ii, $hx_tmp
For $Ii = $hx_array[0] To 1 Step -1
$hx_tmp = StringInStr($HX_REF, $hx_array[$Ii]) - 1
$ret += $hx_tmp * 16 ^ $hx_count
$hx_count += 1
Next
Return $ret
EndFunc ;==>_HexToDec()

; #FUNCTION# ==============================================================
; Function Name..: _DecToHex ( expression [, length] )
; Description ...: Returns a string representation of an integer converted to hexadecimal.
; Parameters ....: expression - The integer to be converted to hexadecimal.
; - [optional] Number of characters to be returned (no limit).
; If no length specified, leading zeros will be stripped from result.
; Return values .: Success - Returns a string of length characters representing a hexadecimal expression, zero-padded if necessary.
; Failure - Returns "" (blank string) and sets @error to 1 if expression is not an integer.
; Author ........: jennico (jennicoattminusonlinedotde)
; Remarks .......: Output format "FFFF".
; The function will also set @error to 1 if requested length is not sufficient - the returned string will be left truncated.
; Be free to modify the function to be working with binary type input - I did not try it though.
; current AutoIt Hex() limitation: 0xFFFFFFFF (4294967295).
; Related .......: Hex(), Dec(), _HexToDec()
; =======================================================================
Func _DecimalToHex($hx_dec, $hx_length = 21)

If IsInt($hx_dec) = 0 Then
SetError(1)
        MsgBox(0,"Error","Wrong input, try again ...")
Return ""
EndIf
Local $ret = "", $Ii, $hx_tmp, $hx_max
If $hx_dec < 4294967296 Then
If $hx_length < 9 Then Return Hex($hx_dec, $hx_length)
If $hx_length = 21 Then
$ret = Hex($hx_dec)
While StringLeft($ret, 1) = "0"
$ret = StringMid($ret, 2)
WEnd
Return $ret
EndIf
EndIf
For $Ii = $hx_length - 1 To 0 Step -1
$hx_max = 16 ^ $Ii - 1
If $ret = "" And $hx_length = 21 And $hx_max > $hx_dec Then ContinueLoop
$hx_tmp = Int($hx_dec/($hx_max+1))
If $ret = "" And $hx_length = 21 And $Ii > 0 And $hx_tmp = 0 Then ContinueLoop
$ret &= StringMid($HX_REF, $hx_tmp+1, 1)
$hx_dec -= $hx_tmp * ($hx_max + 1)
Next
$ret=String($ret)
If $hx_length < 21 And StringLen($ret) < $hx_length Then SetError(1)
Return $ret
EndFunc ;==>_DecToHex()

; ----------------------------------------------------------------
; Hex to Decimal Conversion ; Correct till Decimal 65789 ?!
Func _HexToDecimal_NotCorrect($Input)
Local $Temp, $i, $Pos, $Ret, $Output

If StringRegExp($input,'[[:xdigit:]]') then
$Temp = StringSplit($Input,"")

For $i = 1 to $Temp[0]
    $Pos = $Temp[0] - $i
    $Ret = Dec (Hex ("0x" & $temp[$i] )) * 16 ^ $Pos
    $Output &= $Ret
Next
    return $Output
Else
    MsgBox(0,"Error","Wrong input, try again ...")
    Return
EndIf   
EndFunc

; Decimal To Hex Conversion
Func _DecimalToHex_NotCorrect($Input)
local $Output, $Ret

If StringRegExp($input,'[[:digit:]]') then
Do
    $Ret = Int(mod($Input,16))
    $Input = $Input/16
    $Output = $Output & StringRight(Hex($Ret),1)
Until Int(mod($Ret,16))= 0

    Return StringMid(_StringReverse($Output),2)
Else
    MsgBox(0,"Error","Wrong input, try again ...")
    Return
EndIf   
EndFunc
;-------------------------------------------------------------------

; Binary To Hex
Func _BinaryToHexString($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
Else
    MsgBox(0,"Error","Wrong input, try again ...")
    Return
EndIf   
EndFunc


; Hex To Binary
Func _HexToBinaryString($HexValue)
Local $Allowed = '0123456789ABCDEF'
Local $Test,$n
Local $Result = ''
if $hexValue = '' then
SetError(-2)
Return
EndIf

$hexvalue = StringSplit($hexvalue,'')
for $n = 1 to $hexValue[0]
if not StringInStr($Allowed,$hexvalue[$n]) Then
SetError(-1)
return 0
EndIf
Next

Local $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 _GUICtrlCreateGradient($nStartColor, $nEndColor, $nX, $nY, $nWidth, $nHeight)
Local $color1R = _ColorGetRed($nStartColor)
Local $color1G = _ColorGetGreen($nStartColor)
Local $color1B = _ColorGetBlue($nStartColor)

Local $nStepR = (_ColorGetRed($nEndColor) - $color1R) / $nHeight
Local $nStepG = (_ColorGetGreen($nEndColor) - $color1G) / $nHeight
Local $nStepB = (_ColorGetBlue($nEndColor) - $color1B) / $nHeight

GuiCtrlCreateGraphic($nX, $nY, $nWidth, $nHeight)
For $i = 0 To $nHeight - $nY
$sColor = "0x" & StringFormat("%02X%02X%02X", $color1R+$nStepR*$i, $color1G+$nStepG*$i, $color1B+$nStepB*$i)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, $sColor, 0xffffff)
GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 0, $i)
GUICtrlSetGraphic(-1, $GUI_GR_LINE, $nWidth, $i)
Next
EndFunc

Some of the functions i borrowed from someone -- some of them are mine.

Can't really tell what's what.

Anyhow, I hope it is fool proof.

It wasn' t fool proof, therefore I followed "jennico" advise and used his DecHex and HexDec functions.

Enjoy !!

regards

ptrex

Edited by ptrex
1 person likes this

Share this post


Link to post
Share on other sites



Thanks,

I used the dec to binary in a script of mine.

I'm always confused that the binary() function in AutoIT actually uses hex data

Share this post


Link to post
Share on other sites

Nice work.

I think (With your permission of course) I'll re-work the functions a bit and put them into one of my UDFs (probably a new one).


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

@GEOSoft

Be my guest.

My brain still hurts too much to do onything further with this. :)

PS:

You might add a functions to convert ASC to HEX en visa versa as well

This can be done fairly easy from what I have posted.

regards

ptrex

I already have one to do that. I'll just get everything gatherd up into a single udf.

Withe the functions you posted, I think it's mainly a matter of removing the message boxes and returning an error code instead. I'll work at it later.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

I pulled out the dec to binary, and made an IP / Subnet / Gateway checker.

I realize that there is probably a cleaner, nicer way to do this. Probably using regular expression.

But I needed something quick for some routers.

So Here is what I have.

func _subnet_validation($ip2check,$mask2check,$gw2check)
    
    ;check for ip and gw duplication
    if $ip2check = $gw2check Then
        return 0
    EndIf
    local $bin_ip , $bin_mask, $bin_gw , $bit_ip , $bit_mask , $bit_gw , $end_of_networks 
    local $ip_array = StringSplit($ip2check,".")
    local $mask_array = StringSplit($mask2check,".")
    local $gw_array = StringSplit($gw2check,".")
    if $debug > 0 then MsgBox(0,"ip array[0]",$ip_array[0])
            ;check to make sure each has 4 octets
    if $ip_array[0] <> 4 or $mask_array[0]  <> 4 or $gw_array[0] <> 4 Then
        return 0
    EndIf
    for $loop = 1 to 4
        ;check to make sure they are 0-255
        if $ip_array[$loop] < 0 or $mask_array[$loop] < 0 or $gw_array[$loop] < 0 _ 
                    or   $ip_array[$loop] > 255 or $mask_array[$loop] > 255 or $gw_array[$loop] > 255   Then
            return 0
        EndIf
        
        $bin_ip = _DecToBinary($ip_array[$loop])
        $bin_mask = _DecToBinary($mask_array[$loop])
        $bin_gw = _DecToBinary($gw_array[$loop])
        if $debug > 3 then MsgBox(0,' bin_ip , bin_mask , bin_gw',$bin_ip & " , " & $bin_mask & " , " & $bin_gw)
        ;check bit by bit 
        for $binary_loop = 1 to 8
            $bit_ip = StringMid($bin_ip,$binary_loop,1)
            $bit_mask = StringMid($bin_mask,$binary_loop,1)
            $bit_gw = StringMid($bin_gw,$binary_loop,1)
            if $debug > 4 then MsgBox(0,'bits for ip , mask , gw',$bit_ip & " , " & $bit_mask & " , " & $bit_gw )
            ;check to make sure the mask it good
            if $bit_mask = 0 then $end_of_networks = 1
            ;if we are still part of the network portion
            if $bit_mask = 1 then 
                ;if the mask went to 0, and back to 1, that's bad
                if $end_of_networks = 1 then
                    Return 0
                EndIf
                if $bit_ip <> $bit_gw then 
                    return 0
                EndIf
            EndIf
        Next    
    Next
    return 1
EndFunc

; Decimal To Binary
;code lifted from http://www.autoitscript.com/forum/index.php?showtopic=70507&hl=binary
;thanks ptrex
Func _DecToBinary($iDec)
    Local $i, $extra,$sBinChar = ""
    
    If StringRegExp($iDec,'[[:digit:]]') then
    $i = 1

    Do
     $x = 16^$i
     $i +=1 
     ; Determine the Octects
    Until $iDec < $x
     
    For $n = 4*($i-1) To 1 Step -1
        If BitAND(2 ^ ($n-1), $iDec) Then
            $sBinChar &= "1"
        Else
            $sBinChar &= "0"
        EndIf
    Next
    ;flesh out to 8 characters
    $extra = _StringRepeat("0",8 - StringLen($sBinChar))
    $sBinChar = $extra & $sBinChar
     Return $sBinChar
    Else
        MsgBox(0,"Error","Wrong input, try again ...")
        Return
    EndIf   
EndFunc
1 person likes this

Share this post


Link to post
Share on other sites

@petrex

your converter seems to have an error. i cannot get a conversion from 3933904 integer to hex, returning blank string. you might have to work on that or just use my solution !

cheers j.


Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Share this post


Link to post
Share on other sites

Also Hex doesn't work when using decimal 65280 as an input


My Scripts[topic="73325"]_ReverseDNS()[/topic]Favourite scripts by other members[topic="81687"]SNMP udf[/topic][topic="70759"]Using SNMP - MIB protocol[/topic][topic="39050"]_SplitMon:Section off your monitor!, split your monitor into sections for easy management[/topic][topic="73425"]ZIP.au3 UDF in pure AutoIt[/topic][topic="10534"]WMI ScriptOMatic tool for AutoIt[/topic][topic="51103"]Resources UDF embed/use any data/files into/from AutoIt compiled EXE files[/topic]

Share this post


Link to post
Share on other sites

Simply Good.

Share this post


Link to post
Share on other sites

DecToHex does wrong check this out in calculator:

dec: 76561198720815197

windwows calulator does it good: 11000012D55105D

DecToHex is wrong: 11000012D551060

Any solution for that ?

thanx

Share this post


Link to post
Share on other sites

Confirmed.

@WhitLion -> Search Forum for "dec2hex".


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now