Shaarad Posted June 8, 2012 Share Posted June 8, 2012 Hello everyone, I am trying to write a program which will convert a given string of characters into a binary string. Each character will corrospond to 8 bits....Here is the code I have written. The program goes into an infinite loop, but I am not able to find where. Please help. $string=inputbox ("","Enter string to convert into binary") $binary="" for $i=1 to stringlen($string) $binary&=dectobin(Asc(stringmid($string,$i,1))) Next msgbox (0,"",$binary) func dectobin($decimal) $power=0 while number(power(2,$power))<=number($decimal) $power+=1 WEnd $power-=1 $binary="" $i=$power while $i>=0 if number(power(2,$i))<=number($decimal) Then $binary&="1" $decimal-=power(2,$i) Else $binary&="0" EndIf $i-=1 WEnd return ($binary) EndFunc func power($base,$radix) if $radix=0 Then return (1) EndIf $base2=$base for $i=1 to int($radix)-1 $base*=$base2 Next return ($base) EndFunc Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
JohnOne Posted June 8, 2012 Share Posted June 8, 2012 Something wrong with the StringToBinary() function? AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Shaarad Posted June 8, 2012 Author Share Posted June 8, 2012 When I added a messagebox in the very first for loop showing the current string character being converted, It showed the first character for the first iteration, but null characters for further ones...and then it went into an infinite loop... $string=inputbox ("","Enter string to convert into binary") $binary="" for $i=1 to stringlen($string) msgbox (0,"current character",stringmid($string,$i,1)) $binary&=dectobin(Asc(stringmid($string,$i,1))) Next msgbox (0,"",$binary) this is the outer code...functions are same. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Shaarad Posted June 8, 2012 Author Share Posted June 8, 2012 JohnOne, I saw the StringToBinary function, but I actually wanted to obtain the ones and zeros... when I tried msgbox (0,"",StringToBinary($string)) It show the Hexadecimal output which I don't want.. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
BrewManNH Posted June 8, 2012 Share Posted June 8, 2012 Try it this way, the IntegerToBinary function is from UEZ from the Snippets thread in the Example Scripts section #include <String.au3> $String = "Now is the time" $Binary = "" For $I = 1 To StringLen($String) $Binary &= Integer2Binary(Asc(StringMid($String, $I, 1))) & " | " Next ConsoleWrite($Binary & @lf) Func Integer2Binary($in) ;coded by UEZ If $in = 0 Then Return 0 Local $bin While $in > 0 $bin &= Mod($in, 2) $in = Floor($in / 2) WEnd $bin = _StringReverse($bin) $bin = StringFormat("%08s", $bin) Return($bin) EndFunc If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
elektron Posted June 10, 2012 Share Posted June 10, 2012 Try it this way, the IntegerToBinary function is from UEZ from the Snippets thread in the Example Scripts section #include <String.au3> $String = "Now is the time" $Binary = "" For $I = 1 To StringLen($String) $Binary &= Integer2Binary(Asc(StringMid($String, $I, 1))) & " | " Next ConsoleWrite($Binary & @lf) Func Integer2Binary($in) ;coded by UEZ If $in = 0 Then Return 0 Local $bin While $in > 0 $bin &= Mod($in, 2) $in = Floor($in / 2) WEnd $bin = _StringReverse($bin) $bin = StringFormat("%08s", $bin) Return($bin) EndFunc ^ I guess this would be the simple way to do it using someone's UDF or what not... but here's my version of it that I started writing a couple of days ago when I first saw this post. I made it as easy to read for a beginner as I possibly could. expandcollapse popupmain() ; ConsoleWrite(Bin("a", True)& @LF) Func main() Local $bIsDone = False Do ConsoleWrite("--> Prompting user for string" & @LF) Local $szResult = InputBox("Enter a string", "Please enter a string to convert to binary") If ($szResult = "") Then ; There was an error Select Case @error = 0 ConsoleWrite("The string returned is valid, @error is: " & @error & @LF) Case @error = 1 ConsoleWrite("The cancel button was pushed, @error is: " & @error & @LF) Case @error = 2 ConsoleWrite("The timeout time was reached, @error is: " & @error & @LF) Case @error = 3 ConsoleWrite("The InputBox failed to open (bad args), @error is: " & @error & @LF) Case @error = 4 ConsoleWrite("The InputBox cannot be displayed on any monitor, @error is: " & @error & @LF) Case @error = 5 ConsoleWrite("Invalid parameters width without height or left without top, @error is: " & @error & @LF) Case Else ConsoleWrite("Impossible Happened.") EndSelect Exit Else ConsoleWrite("--> Got data. Here's what the user entered: " & $szResult & @LF) ; $szResult = StringToBinary($szResult, 1); Convert the string to binary data, and return $szConvResult = BinStr($szResult) ConsoleWrite("--> Done converting. Here's what I've got: " & $szConvResult & @LF) MsgBox(0, "Here's the resulting data", $szConvResult) EndIf Until ($bIsDone) EndFunc ;==>main ; Wrapper for Bin() Takes a string and converts it to binary digits Func BinStr($szStringInput) ; Split the string into array of ascii codes that we'll convert to binary ; later. Local $acStringInput = StringSplit($szStringInput, "", 2) ; Split the string into an array Local $result = "" Local $i = 0 For $i = 0 To UBound($acStringInput)-1 Step 1 $result &= Bin($acStringInput[$i]) Next Return $result EndFunc ;==>Bin ; Converts a character to its binary ascii ; equivalent. When $bPad is set to True, it ; will return a padded equivalent of the ; binary sequence (Unsigned) Func Bin($cACharacter, $bPad = True) Local $szReturnValue = "" ; Local $iCode = Asc($cACharacter) ; Grab the ascii code While($iCode) If(Mod($iCode, 2) == 0) Then $szReturnValue = "0" & $szReturnValue Else $szReturnValue = "1" & $szReturnValue EndIf $iCode = Int($iCode/2) WEnd ; At this point, we're done. But... let's say we wanted to pad the string. Here's how I did it. If( $bPad = True ) Then ; Pad the num Dim $aiBinNum[8] = [0, 0, 0, 0, 0, 0, 0, 0] ; Let's create an array of 8 0's to start with Local $acBuf = StringSplit($szReturnValue, "", 2) ; Then split the string we've already generated into an array, 0 indexed (that's what the 2 flag is for) Local $i = 0; For $i = 0 To UBound($acBuf)-1 Step 1 ; We start at UBound($acBuf)-1 because of 0 vs 1 indexing. UBound counts by 1 and we count starting at 0 here, so -1... $aiBinNum[7-$i] = $acBuf[UBound($acBuf)-1-$i] ; Now we're going to walk the array backwards, starting from the right and going to the left. Next $retBuf = "" For $i = 0 To 7 Step 1 $retBuf &= $aiBinNum[$i] ; Concatenate the result Next $szReturnValue = $retBuf EndIf Return $szReturnValue EndFunc Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now