Jump to content

Binary Conversion Tool


Don N
 Share

Recommended Posts

Figured there were already Hex() and Dec() and i use binary a lot in school and work so here is a simple conversion utility and demo script.

The actual conversion script

Bin.au3

Demo script

Bin_example.au3

_____________________________________________________"some people live for the rules, I live for exceptions"Wallpaper Changer - Easily Change Your Windows Wallpaper

Link to comment
Share on other sites

I don't use binary but it works. You should make a function that converts it back

My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

Ok yea it will only work on numbers in the integer range...(-2147483648 to 2147483647)

I guess i will check for that tho....thanks

_____________________________________________________"some people live for the rules, I live for exceptions"Wallpaper Changer - Easily Change Your Windows Wallpaper

Link to comment
Share on other sites

This checks to see if it is in that range you specified...

Func Bin( $dec )
    ;check to see if the number is 0, if it is return 0
    If $dec = 0 Then Return "0"  
    
    ;check to see if number is valid
    If _IsOutsideRange(-2147483648, 2147483647, $dec) = 0 Then Return 0
    
    ;used to determine whether the number is negative or not we convert the absolute 
    ;value of the dec num to binary then reapply neg sign at the end
    $neg = Abs( $dec ) / $dec
    $dec *= $neg
    
    ;convert the integegral part of the number to binary
    $integral = int_bin( Int( $dec ) )
    
    ;convert the decimal part of the number to binary
    $decimal = dec_bin( $dec - Int( $dec ) )
    
    ;assemble the binary number result from the integral and decimal pieces
    Return StringTrimRight( $neg * 1, 1 ) & $integral & $decimal
EndFunc   ;==>Bin

;this function converts the decimal part of the given number to binary

Func dec_bin( $dec_part )
    Local $binary = ""
    
    If $dec_part = 0 Then Return ""
    
    $binary &= "."
    
    While $dec_part <> 0
        $dec_part *= 2
        
        If $dec_part >= 1 Then
            $binary = $binary & "1"
            $dec_part -= 1
        Else
            $binary = $binary & "0"
        EndIf
    WEnd
    
    Return $binary
    
EndFunc   ;==>dec_bin

;this function converts the integer part of the given number to binary

Func int_bin( $int_part )
    Local $binary = ""
    
    If $int_part = 0 Then Return "0"
    
    While $int_part <> 0
        If Mod( $int_part, 2 ) = 0 Then
            $binary = $binary & "0"
        Else
            $binary = $binary & "1"
            $int_part -= 1
        EndIf
        $int_part = Int( $int_part / 2 )        
    Wend
    
    Return _StringReverse( $binary )
EndFunc   ;==>int_bin

Func _IsOutsideRange($lowest, $highest, $num)
    If (Not IsNumber($lowest)) Or (Not IsNumber($highest)) Or (Not IsNumber($num)) Then 
        SetError(1)
        Return 0
    EndIf
    If $num < $lowest Then Return 0
    If $num > $highest Then Return 0
    Return 1
EndFunc
Edited by RazerM
My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

I have made a decimal to binary function and renamed your functions with _ at the start.

example:

#include <array.au3>
#include <string.au3>
$original = InputBox("Decimal > Binary", "Enter a decimal Number.")
$bin = _Bin($original)
$dec = _BinToDec($bin)
MsgBox(0,"Binary","Original = " & $original & @CRLF & "Decimal > Binary = " & $bin & @CRLF & "Binary > Decimal = " & $dec)

Func _Bin( $dec )
    $dec = Number($dec)
;check to see if the number is 0, if it is return 0
    If $dec = 0 Then Return 0 
    
;check to see if number is valid
    If _IsOutsideRange(-2147483648, 2147483647, $dec) = 0 Then Return 0
    
;used to determine whether the number is negative or not we convert the absolute 
;value of the dec num to binary then reapply neg sign at the end
    $neg = Abs( $dec ) / $dec
    $dec *= $neg
    
;convert the integegral part of the number to binary
    $integral = _int_bin( Int( $dec ) )
    
;convert the decimal part of the number to binary
    $decimal = _dec_bin( $dec - Int( $dec ) )
    
;assemble the binary number result from the integral and decimal pieces
    Return StringTrimRight( $neg * 1, 1 ) & $integral & $decimal
EndFunc  ;==>Bin

;this function converts the decimal part of the given number to binary

Func _dec_bin( $dec_part )
    Local $binary = ""
    
    If $dec_part = 0 Then Return ""
    
    $binary &= "."
    
    While $dec_part <> 0
        $dec_part *= 2
        
        If $dec_part >= 1 Then
            $binary = $binary & "1"
            $dec_part -= 1
        Else
            $binary = $binary & "0"
        EndIf
    WEnd
    
    Return $binary
    
EndFunc  ;==>dec_bin

;this function converts the integer part of the given number to binary

Func _int_bin( $int_part )
    Local $binary = ""
    
    If $int_part = 0 Then Return "0"
    
    While $int_part <> 0
        If Mod( $int_part, 2 ) = 0 Then
            $binary = $binary & "0"
        Else
            $binary = $binary & "1"
            $int_part -= 1
        EndIf
        $int_part = Int( $int_part / 2 ) 
    Wend
    
    Return _StringReverse( $binary )
EndFunc  ;==>int_bin

Func _BinToDec($bin)
;reverse for easier use
    $bin = _StringReverse($bin)
;split into 1, 2, 4, 8, 16, 32, 64, 128 etc 'columns'
    $split = StringSplit($bin, "")
;set result var to zero
    $dec = 0
;create column array starting with 0 and 1 (0 isn't needed)
    Dim $decnum[2] = [0, 1]
;check for anything that isnt binary
    For $i = 1 To $split[0]
        If ($split[$i] <> 0) And ($split[$i] <> 1) Then
            SetError(1)
            Return 0
        EndIf
    Next
;add column header to array for only those needed
    For $i = 2 To StringLen($bin)
        _ArrayAdd($decnum, $decnum[$i-1] * 2)
    Next
    #cs
    For column 1 it multiplies 1 by 0 or 1
    For column 2 it multiplies 2 by 0 or 1
    For column 3 it multiplies 4 by 0 or 1
    For column 4 it multiplies 8 by 0 or 1
    For column 5 it multiplies 16 by 0 or 1
    For column n....
    #ce
    For $i = 1 To $split[0]
        $dec += $split[$i] * $decnum[$i]
    Next
    Return $dec
EndFunc

Func _IsOutsideRange($lowest, $highest, $num)
    If (Not IsNumber($lowest)) Or (Not IsNumber($highest)) Or (Not IsNumber($num)) Then 
        SetError(1)
        Return 0
    EndIf
    If $num < $lowest Then Return 0
    If $num > $highest Then Return 0
    Return 1
EndFunc  ;==>_IsOutsideRange
My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...