czardas Posted July 19, 2010 Share Posted July 19, 2010 (edited) I decided it's time to start working in binary. The only problem is I don't seem to be able to find any functions that produce binary. I have tried a few examples such as: Binary() and StringToBinary() etc... I keep getting Hex values. I wish I could explain it. Edit:Hmm, I forgot to ask a question. Never mind, Perhaps I'll just create my own binary functions, along with some tailor made bitwise operations I have planned. Edited July 19, 2010 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
PsaltyDS Posted July 19, 2010 Share Posted July 19, 2010 You have to learn the difference between how the data is encoded. Hex is just a method for displaying data, it doesn't change the data itself: $binTwo = Binary(2) ; INT32 input ConsoleWrite("$binTwo = " & $binTwo & "; Type = " & VarGetType($binTwo) & @LF) $binTwo = Binary(0x00000002) ; Still an int32 input ConsoleWrite("$binTwo = " & $binTwo & "; Type = " & VarGetType($binTwo) & @LF) $binTwo = Binary("0x00000002") ; String, representing a binary value as hex for input ConsoleWrite("$binTwo = " & $binTwo & "; Type = " & VarGetType($binTwo) & @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 Link to comment Share on other sites More sharing options...
czardas Posted July 19, 2010 Author Share Posted July 19, 2010 (edited) Hi PsaltyDS. I understood this, but I find it difficult to work with in some instances. I intend to perform a whole load of operations with binary strings including: spliting, concatenation, triming, stringsearch, bitwise etc. I guess all this is possible, but I find it difficult to visualize what's happening with a hex representation. I have been thinking about what kind of input and return values I would like to use use. I was also considering trying to write some functions which allow the base of each to be set within the function parameters. So for example: I could input decimal, do a binary operation and output octal. I will mainly use binary, dec and hex but I might suddenly wish to have a differnt base as the return value. I was thinking I might need dodecimal at some point, due to it being divisible by the prime factors 1, 2 and 3. By comparison hex only has 2 prime factors, which turns out to be quite awkward in certain situations. Thanks for taking time to respond. Edited July 19, 2010 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
jvanegmond Posted July 19, 2010 Share Posted July 19, 2010 You want to simulate the 0's and 1's in your script since there's nowhere in the software level where you are actually dealing with 0's and 1's (exceptions not included). Probably as an array of booleans or strings with 0 and 1, or a and b, or whatever you prefer. I made some stuff here that deals with this: http://www.autoitscript.com/forum/index.php?showtopic=90061&view=findpost&p=647530 Enjoy. github.com/jvanegmond Link to comment Share on other sites More sharing options...
czardas Posted July 19, 2010 Author Share Posted July 19, 2010 (edited) Yes, you are correct. It is not the numerical values of 0 and 1 that are important to me, but rather the two states: on or off (true or false). Boolean is the appropriate description. I still need to absorb and understand the methods at my disposal. Thanks for the link. I'm pretty sure that looking at what you've done will prove useful to me. Edited July 19, 2010 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
czardas Posted July 20, 2010 Author Share Posted July 20, 2010 For the record, I thought I ought to illustrate what I am getting at with some code. Take the following example. $Dec = 255 $Bin = Binary($Dec) $ret = StringTrimRight($Bin, 4) MsgBox(0, "Trimmed Binary String", $ret) In my mind, the result should be either 'F' or '1111'. The only conclusion I can draw from this is that $Bin is not being treated as a true binary string. I have decided to just deal with my immediate concerns, though I may at some point in the future create some more general number base functions. This is what I have so far. I will add various binary string manipulation functions later. expandcollapse popup#include <Array.au3> Local $asCol0[16] = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"] Local $asCol1[16] = ["0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"] Local $asHex[16][2] For $i = 0 To 15 $asHex[$i][0] = $asCol0[$i] $asHex[$i][1] = $asCol1[$i] Next $asCol0 = 0 $asCol1 = 0 Func _HexToBin($sString) ; Convert from Hex to Binary If StringIsXDigit ($sString) = 0 Then Return SetError(1, 0, "") Local $asTemp = StringSplit($sString,"",2) For $i = 0 To UBound($asTemp) -1 For $j = 0 To 15 If $asTemp[$i] = $asHex[$j][0] Then $asTemp[$i] = $asHex[$j][1] ExitLoop EndIf Next Next Return _ArrayToString($asTemp, "") EndFunc Func _BinToHex($sString) ; Convert from Binary to Hex If StringLen($sString) < 1 Or StringRegExp($sString, "[^0-1]") = 1 Then Return SetError(1, 0, "") Local $iRemainder = Mod(StringLen($sString), 4) If $iRemainder > 0 Then For $i = 1 To 4 - $iRemainder $sString = "0" & $sString Next EndIf Local $asTemp[StringLen($sString)/4] For $i = 0 To UBound($asTemp) -1 $asTemp[$i] = StringLeft($sString, 4) $sString = StringTrimLeft($sString, 4) Next For $i = 0 To UBound($asTemp) -1 For $j = 0 To 15 If $asTemp[$i] = $asHex[$j][1] Then $asTemp[$i] = $asHex[$j][0] ExitLoop EndIf Next Next Return _ArrayToString($asTemp, "") EndFunc Perhaps this will be of interest to someone, so why not post it here. @ Manadar, your post was helpful. Thx. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
trancexx Posted July 20, 2010 Share Posted July 20, 2010 This is very confusing to me. @czardas, could you help me understand how should 255 be turned to F or 1111. What logic is that? Or is that in combination with that String... function? You want to check what bits are set, is that it? $iValue = 255 ConsoleWrite(_IntToBin($iValue) & @CRLF) Func _IntToBin($iValue) Local $sOut For $i = 31 To 0 Step -1 $sOut &= BitAND(BitShift($iValue, $i), 1) Next Return $sOut EndFunc ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
czardas Posted July 20, 2010 Author Share Posted July 20, 2010 (edited) Trancexx, thanks for the illustration. Yes it had to do with the string function. The result I expected with the code was to trim the last four characters. So '11111111' becomes '1111'. That was merely an example. I guess it's my lack of experience with bitwise operations that is presenting problems for me. The actual binary strings I intend to use will have no more than 12 digits. 32 bit values will require triming. I am so used to manipulating strings (also on my guitar) that I can't see beyond those functions. The idea is very simple: '000010001001' represents a major chord (Doh Mi So). I can modify the chord in various ways. For example I can add another note by changing one of the zeros to a 1. Or I can shift a 1 to the left or right (flattening or sharpening the note). All possible harmonic variations can be represented with 3 hexidecimal digits. So the major chord above has the hex ID - 089. The black and white keys of a piano look like this: 101011010101 (Doh Re Mi Fa Sol La Te) or hex ID - AD5. The system is also modular. The major chord above has 3 variants '000010001001', '000100100001' or '001000010001'; and the 7 note scale above has 7 variants (called inversions). To understand this just imagine the 12 digit binary string looped around like the numbers on a clock face. I just rotated the string. Using binary representation, I can do just about anything. I thought bitwise operations would be most useful for this purpose, but I find them kind of confusing. So I wondered if I ought to write my own (to just handle 12 digits). One design choice I made, was to always make the last digit = 1. So all the harmonic ID values will be odd numbers. This reduces the number of variations by half. At this stage I am not concerned with the absolute position of any particular character (due to the modular nature of the system), but rather the relative positions between each character. I am still unsure how I will proceed with some aspects of this setup. I have all the data, but handling it is confusing me. Most of the time I will be compairing binary strings for exact (or close) matches, but the data will be stored as hex. I hope some of this makes sense. If not, perhaps I'm not explaining it very well. I will try and understand your example. You make it look so easy. Anyway, regardless of how data is stored on disk. it is boolean values that I intend to use, and binary representation fits the criteria. If I can find some shortcuts then I'll use them. At the moment I just want to get started on the project. Edited July 20, 2010 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Spiff59 Posted August 1, 2010 Share Posted August 1, 2010 (edited) I was just looking for a routine to convert a string like "00110101" into it's numeric value of decimal 53 or hex 35. I didn't run across one in a short search, so threw one together. There may be a simpler way to go about this,but this seems pretty straightforward So, to compliment trancexx's _IntToBin(), here's a _BinToInt(): Func _BinToInt($sValue) Local $iOut = 0, $aValue = StringSplit($sValue, "") For $i = 1 To $aValue[0] $aValue[0] -= 1 If $aValue[$i] = "1" Then $iOut += 2 ^ ($aValue[0]) Next Return $iOut EndFunc Edit: Initialize $iOut to zero Edited October 13, 2010 by Spiff59 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