Sign in to follow this  
Followers 0
ConsultingJoe

Bin2Dec UDF

7 posts in this topic

This is what I have so far, I'm not that great with arrays.

I need this so I can convert the dec to hex after the bin2dec finishes

Its for my relay board program I am righting

bin2dec()
Func bin2dec()
$bin = "1001"
if StringIsDigit ( $bin ) = 0 then return -1
$binsplit = StringSplit ( $bin, "")
msgbox(0,"",$binsplit[0])
$start = 1
For $i = $binsplit[0] To 1 Step -1
if $binsplit[$i] = 1 then
$start = $start * 2
$num = $start + $binsplit[$i]
msgbox(0,"",$num)
endif
Next
endfunc

Relay board program

#include <GUIConstants.au3>
;OFF = 0
;ON  = 1
GUICreate("MY RELAY", 260, 80)

$b1 = GUICtrlCreateButton (1, 10, 10, 30)
$b2 = GUICtrlCreateButton (2, 40, 10, 30)
$b3 = GUICtrlCreateButton (3, 70, 10, 30)
$b4 = GUICtrlCreateButton (4, 100, 10, 30)
$b5 = GUICtrlCreateButton (5, 130, 10, 30)
$b6 = GUICtrlCreateButton (6, 160, 10, 30)
$b7 = GUICtrlCreateButton (7, 190, 10, 30)
$b8 = GUICtrlCreateButton (8, 220, 10, 30)
$allon = GUICtrlCreateButton ("All ON", 70, 55, 60)
$alloff = GUICtrlCreateButton ("All OFF", 130, 55, 60)
$l1 = GUICtrlCreateLabel ("0", 20, 40)
$l2 = GUICtrlCreateLabel ("0", 50, 40)
$l3 = GUICtrlCreateLabel ("0", 80, 40)
$l4 = GUICtrlCreateLabel ("0", 110, 40)
$l5 = GUICtrlCreateLabel ("0", 140, 40)
$l6 = GUICtrlCreateLabel ("0", 170, 40)
$l7 = GUICtrlCreateLabel ("0", 200, 40)
$l8 = GUICtrlCreateLabel ("0", 230, 40)
GUISetState ()


; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
if $msg = $b1 then
        process()
    endif
if $msg = $b2 then
        process()
    endif
if $msg = $b3 then
        process()
    endif
if $msg = $b4 then
        process()
    endif
if $msg = $b5 then
        process()
    endif
if $msg = $b6 then
        process()
    endif
if $msg = $b7 then
        process()
    endif
if $msg = $b8 then
        process()
    endif
if $msg = $alloff then
For $i = 13 to 20
GUICtrlSetData ( $i, "0" )
Next        
    endif
if $msg = $allon then
For $i = 13 to 20
GUICtrlSetData ( $i, "1" )
Next
    endif
Wend

Func process()
$msg = $msg + 10
If GUICtrlRead($msg) = "1" Then
GUICtrlSetData ( $msg, "0" )
Else
GUICtrlSetData ( $msg, "1" )
Endif

endfunc

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Share this post


Link to post
Share on other sites



Okay... you said in your subtitle, that "I cant get it", but then you fail to explain in any amount of detail what you cant get. Are you needing a Binary to Dec function? I can create one. It seems you have a decent idea of it, but a little explanation of what you need help with would be great.

Code optimization?

Something a little more explanatory would get you more responses.

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

I hope this helps. I just whipped this up for you.

MsgBox(0, "TEST", _BinToDec(10))

;===============================================================================
;
; Function Name:    _BinToDec("binary")
; Description:    Converts a binary number to a decimal number.
; Parameter(s):  $i_Binary is the binary number to be converted.
; Requirement(s):   Binary Input.
; Return Value(s):  On Success - Returns the converted binary.
;                  On Failure - -1  and sets @ERROR = 1
; Author(s):        Jarvis Stubblefield
;
;===============================================================================

Func _BinToDec($i_Binary)
    Local $i_Array, $i_Decimal
    
    If IsInt($i_Binary) Then
        $i_Array = StringSplit($i_Binary, "")
        
        For $i = $i_Array[0] To 1 Step -1
            $i_temp = $i_Array[$i]*2^($i_Array[0] - $i)
            $i_Decimal = $i_Decimal + $i_temp
        Next
        
        Return $i_Decimal
    Else
        Return -1
        SetError(1)
    EndIf
EndFunc

Let me know if you have any further questions...

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

I hope this helps. I just whipped this up for you.

MsgBox(0, "TEST", _BinToDec(10))

;===============================================================================
;
; Function Name:    _BinToDec("binary")
; Description:    Converts a binary number to a decimal number.
; Parameter(s):  $i_Binary is the binary number to be converted.
; Requirement(s):   Binary Input.
; Return Value(s):  On Success - Returns the converted binary.
;                  On Failure - -1  and sets @ERROR = 1
; Author(s):        Jarvis Stubblefield
;
;===============================================================================

Func _BinToDec($i_Binary)
    Local $i_Array, $i_Decimal
    
    If IsInt($i_Binary) Then
        $i_Array = StringSplit($i_Binary, "")
        
        For $i = $i_Array[0] To 1 Step -1
            $i_temp = $i_Array[$i]*2^($i_Array[0] - $i)
            $i_Decimal = $i_Decimal + $i_temp
        Next
        
        Return $i_Decimal
    Else
        Return -1
        SetError(1)
    EndIf
EndFunc

Let me know if you have any further questions...

JS

WOW THANK YOU SO MUCH. THATS IT

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Share this post


Link to post
Share on other sites

Glad I was able to help. I will submit this as a UDF to be added to the beta.

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

Glad I was able to help. I will submit this as a UDF to be added to the beta.

JS

Hi sorry to bother you but can it work with a string because it keeps giving me -1

want to use something like "0011101"

The Number func trims the 00 off and puts "11101" into the func


[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Share this post


Link to post
Share on other sites

Hi sorry to bother you but can it work with a string because it keeps giving me -1

want to use something like "0011101"

The Number func trims the 00 off and puts "11101" into the func

It doesnt matter... any zero(0)'s before the 1's dont count anyways. I put both the numbers you entered, and it worked great. 11101, and 0011101.

If you could give more specifics it would be great. I cant fix something that I see as working :lmao:

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

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
Sign in to follow this  
Followers 0