Mat Posted April 16, 2009 Posted April 16, 2009 Basically, StringToBinary was confusing me, as it was returning stuff like 0x3234 for 42, when i expected 010101...so i wrote this!very simple: BinaryEx.au3and a little GUI for converting: BinaryConverter.au3Thought it might come in handy to someone somewhere... so released it.Thanks to sublime, who had the job of explaining StringToBinary....MDiesel AutoIt Project Listing
JRSmile Posted April 16, 2009 Posted April 16, 2009 i did thought about something like this before, for using it with my Supernet calculator. but when it comes to "how much bits are needed to represent this ammount of ips i remembered about good old maths: Func convert($a) if $a < 2 then Return 0 Return Ceiling(Log($a)/Log(2)) EndFunc it can be used to get the ammount of bits used in an subnetmask to represent a ip range for example: if you need 16 ipaddresses the function returns you 4 because you need 4 bits to create 16 combinations :-) maybe this comes in handy for you. its amazing how far people can be away from each other having slightly similar ideas $a=StringSplit("547275737420796F757220546563686E6F6C75737421","") For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4) Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));''Chr("a")&"HI" Next ;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile; MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)
Marlo Posted April 16, 2009 Posted April 16, 2009 Indeed tis confuseing, i had to resort to using this function for extracting numbers from binary: Func _BinaryToInt($4bytes) $Integer = DllStructCreate("int") $Binary = DllStructCreate("byte[4]", DllStructGetPtr($Integer)) DllStructSetData($Binary, 1, $4bytes) Return DllStructGetData($Integer, 1) EndFunc Click here for the best AutoIt help possible.Currently Working on: Autoit RAT
Mat Posted April 16, 2009 Author Posted April 16, 2009 (edited) @JRSmile kk, thats useful, I would have done it something more like: Func GetBinLength ($Num) Local $i = 1 Local $Return = 0 Do $Return += 1 $i *= 2 Until $i > $Num Return $Return EndFunc so thanks! your way is a lot better, I haven't looked much at maths functions... oh and, why can't I seem to make a for loop with step *2? is this just not allowed? Func GetBinLength ($Num) Local $Return = 0 For $i = 1 to $Num Step *2 $Return += 1 Next Return $Return EndFunc Edited April 16, 2009 by mdiesel AutoIt Project Listing
Mat Posted April 16, 2009 Author Posted April 16, 2009 (edited) Sorry Marlo, started replying before you posted... once again my way is less efficient (i think) but I can understand it!! I don't know much about dll's either, so my code just does string split "" and if 1 then add to the number!! EDIT: I think I misunderstood you sorry.... what exactly does it do? I've tested but can't work it out, what is the input supposed to be? Edited April 16, 2009 by mdiesel AutoIt Project Listing
Authenticity Posted April 16, 2009 Posted April 16, 2009 Take a look here you'll see the logic behind converting decimal to binary and vice versa.
jvanegmond Posted April 16, 2009 Posted April 16, 2009 (edited) Hey mdiesel, nice little app there. The reason we don't use 010011 numbers or strings in computing is because it is much easier to describe all the values in one or several bytes. The octal (8-base, one byte) an hexadecimal (16-base, two bytes) is much more convenient for this. Say if you wanted to write the value 0000 0100 0101 0111 you would just write 0x457 .. (1+2+4+16+64+1024) This is easier for the computer to understand because it doesn't need to translate a string into a value to put into memory. Edited April 16, 2009 by Manadar github.com/jvanegmond
SkinnyWhiteGuy Posted April 16, 2009 Posted April 16, 2009 (edited) Uh, octal and hexadecimal has nothing to do with 1 byte or 2 bytes. Hex can show just one byte (actually, just like this: 0x10). The main thing I think people are having problems understanding is that the Binary that is in Autoit isn't the binary numbering system, but the raw bytes that would normally be kept in memory, and is usually hidden from users. A string is nothing but a formatted output for the Binary representing it. The same applies for all values we take for granted. Binary as it's name makes sense to me. What else would you call it, StringToRawMemoryValues? Just remember, Binary doesn't mean the binary number system, but the raw data as the computer see's it. Edit: Grammar ftw! Edited April 16, 2009 by SkinnyWhiteGuy
Mat Posted April 16, 2009 Author Posted April 16, 2009 It makes sense once its explained, and this is released to work with the number system. @AuthenticCity Took a while, but heres my autoit version. $input = 42 $Count = "" While ($input > 0) $Count &= Mod ($Input, 2) If Not IsInt ($input/2) Then $Input -= 1 $input /= 2 Wend MsgBox (48, "Test", $Count); should be 010101 AutoIt Project Listing
Ascend4nt Posted April 16, 2009 Posted April 16, 2009 mdiesel, Negative numbers will pose a problem, and the Bitxx() functions only work on 32-bits currently. (see my signature for solutions to both) Also, if you want to check if a *string* is all 0's or 1's, this will work: StringRegExp("01010","^[01]+$",0) I haven't written a _BinaryStrToNum() function yet as I haven't come across anything where I need to do such a conversion, but it's quite possible to do up to 64-bit calculations, though anything in the 64th bit position will automatically make the number negative. My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code)
Authenticity Posted April 16, 2009 Posted April 16, 2009 $input = 42 $Count = "" While ($input > 0) $Count &= Mod ($Input, 2) If Not IsInt ($input/2) Then $Input -= 1 $input /= 2 Wend MsgBox (48, "Test", $Count); should be 010101 $Count contain the reverse string. You'll need to use _StringReverse to get the correct binary presentation.
Marlo Posted April 16, 2009 Posted April 16, 2009 Sorry Marlo, started replying before you posted... once again my way is less efficient (i think) but I can understand it!! I don't know much about dll's either, so my code just does string split "" and if 1 then add to the number!! EDIT: I think I misunderstood you sorry.... what exactly does it do? I've tested but can't work it out, what is the input supposed to be? Well i use the function in my TCP servers because when i send or recieve information on my server i prefix a string with an integer (to use as a command) So to read the command i'll use something like: $iCmd = _BinaryToInt(BinaryMid($BinaryVar, 1, 4)) This just reads the first 4 bytes from a piece of binary and converts it into an integer Click here for the best AutoIt help possible.Currently Working on: Autoit RAT
Mat Posted April 16, 2009 Author Posted April 16, 2009 right, just got back and... @Ascendant yep, makes my last function practically pointless (as is now said in the script)... I don't expect it to work with negatives or 64 bits... It was only supposed to be simple, and as I have said, it has been done before (and probably will be again). Thanks for the info though. @Authenticity ah... I see exactly what you mean, It's couse I started at 1 and added on the way up... How come I'm getting the right results then? I could do string reverse, or just: (Doesn't give the right results though...) $input = 42 $Count = "" While ($input > 0) $Count = Mod ($Input, 2) & $Count If Not IsInt ($input/2) Then $Input -= 1 $input /= 2 Wend MsgBox (48, "Test", $Count); should be 010101 @Marlo oh... I get it now. AutoIt Project Listing
Authenticity Posted April 17, 2009 Posted April 17, 2009 Well, now it's OK. $Count &= something is not the same as $Count = Something & $Count, and it should be 101010 because it's from the most significant to the least significant.
Mat Posted April 17, 2009 Author Posted April 17, 2009 Well, now it's OK. $Count &= something is not the same as $Count = Something & $Count, and it should be 101010 because it's from the most significant to the least significant.Yep got that... Thanks authenticity, you learn something new every day (even in the school holidays )Thanks to everyone else too! AutoIt Project Listing
SkinnyWhiteGuy Posted April 17, 2009 Posted April 17, 2009 Well, since I just had to do this the other day, here's a converter for Autoit's Binary to numerical binary. Func BinaryToBits($bin) Local $output For $i = 1 To BinaryLen($bin) For $j = 7 To 0 Step -1 $output &= BitAND(BitShift(BinaryMid($bin, $i, 1), $j), 1) Next Next Return $output EndFunc
Mat Posted April 17, 2009 Author Posted April 17, 2009 nicely done... I probably would have gone with binary to string and then string to binary.... completely rewritten both scripts (OK, not completely then...) anyway, re done most functions with new info. AutoIt Project Listing
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