Sign in to follow this  
Followers 0
Nevin

decimal to binary conversion

8 posts in this topic

#1 ·  Posted (edited)

Sorry to ask, but I can't find a thread on this.

If you run this

$char = "A"
consolewrite(asc($char))
$bah = asc($char)
msgbox(0,"",Binary($char))

65 is written to the console, which is the ASCII value of the letter A. Then try to convert 65 to binary and you get 0x41 which shows up in the msgbox. 0x41? What the heck? That's hexadecimal.

Is there a function to convert a decimal number to binary, and back? A user needs to input a word, then it needs to be reduced to binary, a few functions are performed, and then it needs to go back to the new word. i.e. ASCII chars > ASCII values > binary > functions performed > ASCII values > ASCII chars.

Edited by Nevin

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Sorry to ask, but I can't find a thread on this.

If you run this

$char = "A"
consolewrite(asc($char))
$bah = asc($char)
msgbox(0,"",Binary($char))

65 is written to the console, which is the ASCII value of the letter A. Then try to convert 65 to binary and you get 0x41 which shows up in the msgbox. 0x41? What the heck? That's hexadecimal.

Is there a function to convert a decimal number to binary, and back? A user needs to input a word, then it needs to be reduced to binary, a few functions are performed, and then it needs to go back to the new word. i.e. ASCII chars > ASCII values > binary > functions performed > ASCII values > ASCII chars.

Hex is the way binary is written down, for convienence. Each character in hex fully describes exactly four bits.

P.S. While StringFormat() will do octal for you, a leading zero does not make a number octal in AutoIt, like it does in the DOS command line, for example.

In some contexts, 17 is a decimal 17, 017 is octal (15 decimal), 0x17 is hex (23 decimal), and 0b10111 would be binary for 0x17. AutoIt does not have all those formats available. For example, you can use decimal, hex, or octal with StringFormat(), but no binary format.

If you must have ones and zeros, just do a quick loop to convert the hex characters directly to a string of 1 and 0 characters.

:)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I forgot who made this & in the code is not said who, so i cant say who to thank fr this, but i got it from this forum under sample scripts so decompile the exe & you will see the code.

A side note im not really sure if the normal text to hex conversion works 100% hire, last time i tried, I had problems with it, so i used HEX Workshop built in tool for conversion.

EDIT: attachment removed & replaced with code...runninglow on attachment space.

#include <GUIConstants.au3>

GuiCreate("Converter", 392, 323,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
$Label_1 = GuiCtrlCreateLabel("Input:", 10, 10, 80, 20)
$Edit_2 = GuiCtrlCreateEdit("", 10, 30, 360, 100)
$Label_3 = GuiCtrlCreateLabel("Output:", 10, 140, 40, 20)
$Edit_4 = GuiCtrlCreateEdit("", 10, 160, 360, 100)
$Button_5 = GuiCtrlCreateButton("Binary To Text", 10, 270, 120, 20)
$Button_6 = GuiCtrlCreateButton("Octal To Text", 140, 270, 120, 20)
$Button_7 = GuiCtrlCreateButton("Hex To Text", 270, 270, 110, 20)
$Button_8 = GuiCtrlCreateButton("Text To Binary", 10, 300, 120, 20)
$Button_9 = GuiCtrlCreateButton("Text To Octal", 140, 300, 120, 20)
$Button_10 = GuiCtrlCreateButton("Text To Hex", 270, 300, 110, 20)
GuiSetState()
While 1
    Switch GuiGetMsg()
    Case $GUI_EVENT_CLOSE
        ExitLoop
    Case $Button_8
        GUICtrlSetData($Edit_4, _InToOut(GUICtrlRead($Edit_2), 2))
    Case $Button_9
        GUICtrlSetData($Edit_4, _InToOut(GUICtrlRead($Edit_2), 8))
    Case $Button_10
        GUICtrlSetData($Edit_4, _InToOut(GUICtrlRead($Edit_2), 16))
    Case $Button_5
        GUICtrlSetData($Edit_4, _OutToIn(GUICtrlRead($Edit_2), 8))
    Case $Button_6
        GUICtrlSetData($Edit_4, _OutToIn(GUICtrlRead($Edit_2), 3))
    Case $Button_7
        GUICtrlSetData($Edit_4, _OutToIn(GUICtrlRead($Edit_2), 2))
    EndSwitch
WEnd
Exit
Func _InToOut($input, $mode)
    $ss = StringSplit($input, "")
    $output = ""
    For $i = 1 to $ss[0]
        $output &= _ConvertInToOut($ss[$i], $mode)
    Next
    Return $output
EndFunc
Func _ConvertInToOut($in, $mode)
    Switch $mode
    Case 16
        Return Hex(Asc($in), 2)
    Case 2
        Return _BinaryInToOut($in)
    Case 8
        Return _OctalInToOut($in)
    EndSwitch
EndFunc
Func _BinaryInToOut($in)
    $in = Asc($in)
    Local $t1 = ""
    For $i = 1 to 8
        $t1 = Mod($in, 2) & $t1
        $in = Floor($in/2)
    Next
    Return $t1
EndFunc
Func _OctalInToOut($in)
    $in = Asc($in)
    Local $t1 = ""
    For $i = 1 to 3
        $t1 = Mod($in, 8) & $t1
        $in = Floor($in/8)
    Next
    Return $t1
EndFunc
Func _OutToIn($input, $mode)
    $temp = ""
    $ss = StringSplit(_ProcessOutToInInput($input), "")
    $output = ""
    MsgBox(0,$ss[0], $ss[0]/$mode)
    For $i = 1 to $ss[0] Step $mode
        $temp = ""
        For $j = 0 To $mode-1
            $temp &= $ss[$i+$j]
        Next
        $output &= _ConvertOutToIn($temp, $mode)
    Next
    Return $output
EndFunc
Func _ConvertOutToIn($in, $mode)
    Switch $mode
    Case 2
        Return Chr(Dec($in))
    Case 8
        Return _BinaryOutToIn($in)
    Case 3
        Return _OctalOutToIn($in)
    EndSwitch
EndFunc
Func _BinaryOutToIn($in)
    $t1 = 0
    $in = StringSplit($in, "")
    For $i = 1 to $in[0]
        $t1 *= 2
        $t1 += Number($in[$i])
    Next
    Return Chr($t1)
EndFunc
Func _OctalOutToIn($in)
    $in = StringSplit($in, "")
    Local $t1 = 0
    For $i = 1 to $in[0]
        $t1 *= 8
        $t1 += Number($in[$i])
    Next
    Return Chr($t1)
EndFunc
Func _ProcessOutToInInput($in)
    Return StringRegExpReplace($in, "[^:xdigit:]", "")
EndFunc
Edited by goldenix

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Took a look at the code, I'll have to read up on a few functions he calls in there. Thanks.

@Psalty, Yes I do need binary numbers, what is involved in a loop like that? I'm guessing some modulus or something like that?

To me, calling a function binary which gives you hexadecimal values is retarded, but I guess that's just the ignorant noob in me. lol

I tried to pull some of the "Text to binary" functions out of that code:

this is called

Func _InToOut($input, $mode)
    $ss = StringSplit($input, "")
    $output = ""
    For $i = 1 to $ss[0]
        $output &= _ConvertInToOut($ss[$i], $mode)
    Next
    Return $output

which seems to call this

Func _ConvertOutToIn($in, $mode)
    Switch $mode
    Case 2
        Return Chr(Dec($in))
    Case 8
        Return _BinaryOutToIn($in)
    Case 3
        Return _OctalOutToIn($in)
    EndSwitch
EndFunc

which in turn may call these


Func _BinaryOutToIn($in)
    $t1 = 0
    $in = StringSplit($in, "")
    For $i = 1 to $in[0]
        $t1 *= 2
        $t1 += Number($in[$i])
    Next
    Return Chr($t1)
EndFunc
Func _OctalOutToIn($in)
    $in = StringSplit($in, "")
    Local $t1 = 0
    For $i = 1 to $in[0]
        $t1 *= 8
        $t1 += Number($in[$i])
    Next
    Return Chr($t1)
EndFunc

Whatever that means. I'm beginning to think I should make a massive table where I assign each ASCII value it's binary value, like

10 = 1010

9 = 1001

8 = 1000

etc. In fact, I guess I'll get started on that now...how tedious.

I'm beginning to think I should have picked another language for this project lol.

Edited by Nevin

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Maybe you should shed some light on what is exactly that you are trying to do, what are these "functions performed".

Because as of right now I get strong impression you're trying to do things you don't really need to be doing, mostly because you have wrong definition of "binary".

Edited by Siao

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

Here's something to play with:

While 1
    $sChar = InputBox("Char to Binary", "Enter a character: ")
    If @error Or $sChar = "" Then Exit
    ConsoleWrite("Debug: $sChar = " & $sChar & "  Type = " & VarGetType($sChar) & @LF)
    $iDecChar = Asc(StringLeft($sChar, 1))
    ConsoleWrite("Debug: $iDecChar = " & $iDecChar & "  Type = " & VarGetType($iDecChar) & @LF)
    $sHexChar = "0x" & Hex($iDecChar, 2)
    ConsoleWrite("Debug: $sHexChar = " & $sHexChar & "  Type = " & VarGetType($sHexChar) & @LF)
    $sBinChar = "0b"
    For $n = 7 To 0 Step -1
        If BitAND(2 ^ $n, $iDecChar) Then 
            $sBinChar &= "1"
        Else
            $sBinChar &= "0"
        EndIf
    Next
    ConsoleWrite("Debug: $sBinChar = " & $sBinChar & "  Type = " & VarGetType($sBinChar) & @LF)
WEnd

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

thank you thank you thank you thank you thank you :)

i cut it down to this real quick and this works, but I'm sure I can clean it more:

$sChar = InputBox("Char to Binary", "Enter a character: ")
$iDecChar = Asc($sChar)
$sBinChar = ""
For $n = 7 To 0 Step -1
    If BitAND(2 ^ $n, $iDecChar) Then
        $sBinChar &= "1"
    Else
        $sBinChar &= "0"
    EndIf
Next
Msgbox(0,"Tada",$sBinChar)

I don't suppose you have a binary-text version you found that at? lol

edit: cut the code down a bit more

edit2: I've now successfully integrated the text-binary part into the program. Now I'm just afraid of later when I have to go back ;) If anyone has a loop handy for that I'd love to see it. This crazy exponent math is great but it's over my head.

Edited by Nevin

Share this post


Link to post
Share on other sites

This crazy exponent math is great but it's over my head.

No, it's not. You're being lazy. But I'm in a tolerant mood, and have good coffee this morning...

$sChar = InputBox("Char to Binary", "Enter a character: ")
$iDecChar = Asc($sChar)
$sBinChar = ""
For $n = 7 To 0 Step -1
    If BitAND(2 ^ $n, $iDecChar) Then
        $sBinChar &= "1"
    Else
        $sBinChar &= "0"
    EndIf
Next
ConsoleWrite("Sring representation of binary: " & $sBinChar & @LF)

$iASC = 0
For $n = 0 To 7
    If StringMid($sBinChar, 8 - $n, 1) = "1" Then $iASC += 2 ^ $n
Next
ConsoleWrite("Number again: " & $iASC & @LF)

$sNewChar = Chr($iASC)
ConsoleWrite("ASCII character: " & $sNewChar & @LF)

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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