how does binary data work exactly?
Posted 28 February 2011 - 12:11 PM
Posted 28 February 2011 - 12:34 PM
maybe if you give us an example we'll be able to help you.
Posted 28 February 2011 - 12:39 PM
Posted 28 February 2011 - 12:45 PM
Example List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _DesktopDimensions() • _DisplayPassword() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringIsValid() • _StringReplaceWholeWord() • _StringStripChar() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • AutoIt Search • AutoIt3 Portable • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • FileInstallr • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIGetBkColor() • LockFile() • PasteBin • SciTE Jump • Signature Creator • WM_COPYDATA • More Examples...Updated: 11/04/2013
Posted 28 February 2011 - 01:17 PM
How does binary data work in AutoIt?
First you have to try and think of what "binary data" means exactly. Once you understand that, I'll explain how AutoIt allows you to work with it. The easiest way to think binary data it is if you already know what an array is, in that case you can think of binary data as just an array of bytes. And in fact, the terms binary data and array of bytes are almost always interchangeable. In binary data, each byte has an index in the array and a corresponding value between 0 and 255 (a byte).
You can safely assume that AutoIt treats binary data internally as an array of bytes as well. But on the outside, it doesn't look that way because AutoIt does not show you an array of variants that contain bytes (there is no byte data type - this is also the reason why it is called binary data, and not array of bytes), you are forced to use some "tricks" (coming from languages that do have a byte data type) to handle the data. Instead of that, AutoIt offers a few functions to manipulate binary data without exposing the data directly: BinaryLen (returns the size of the array of bytes), BinaryMid (lets you pick part of the array of bytes with a start and length) and finally BinaryToString, which is an interesting one. AutoIt allows you to create string representations of arrays of bytes by using String function, and create arrays of bytes from strings with the Binary function.
Firstly how to create some binary data (or you can read: array of bytes - which is used internally):
$var = Binary("0x00204060")
This will parse the 0x00204060 string and put into memory this array of bytes: 0, 20, 40, 60 (here the bytes are represented as integers, delimited by commas - this is just another way of writing the same thing, but hopefully more human readable). The BinaryLen function on $var will return 4, because there are 4 bytes in our array of bytes. Now let's say we don't really want the first byte, we can use BinaryMid (the array starts on 1 for this function, so 2 indicates skip the first byte):
$var = BinaryMid($var, 2)
The variable $var will now contain the array of bytes: 20, 40, 60. BinaryLen function on $var will now return 3. Finally, we can get our original string representation again by doing this:
MsgBox(0, "", $var)
Which is the same as (because internally AutoIt automatically casts the binary data to string):
Now you may be confused because if String does that job, what does BinaryToString do? Strings can be considered arrays of characters and because bytes can be cast into characters (depending on the size of your byte and your char .. internally), you can also convert an array of bytes into a string. Take the following example:
It puts the bytes 48, 65, C6, C6, F2, etc. into an array of bytes, then into an array of characters (string), which is then printed as a string in the MsgBox function. The end result is a string with the characters: H (48), e (65), l (C6), l (C6), o (F2) etc. Now you understand how arrays of characters and arrays of bytes work, then you must know that the Binary function also accepts an array of characters (string) to turn into binary data. You can do this for example:
This will print: 0x48656C6C6F20576F726C64, but you may get the original message by:
As a final word, I want to point out that there are a lot of ways to represent the same data. The same data in a different representation will often give it a different meaning. The distinction is most obvious when we represent a byte, it can be as 0s and 1s: 00110000 means nothing while 48 means an integer, 0x30 is the same integer as hexadecimal, H (48) represented as a character, or even true represented as a boolean. They're all the same thing to the computer, but they mean different things to us. It's important that you always distinguish data (without context) and information (with context).
It's also convenient to keep in mind that AutoIt internally always represents data in the simplest way possible (think: least processor instructions), and will only convert to another data type when it is necessary (for example when printing). For all you know, when you use Binary("Hello World") AutoIt may keep the string "Hello World" in memory and simply set it to variant type of binary data, so that when the data is read it is converted into an array of bytes first (depending on what you need), if you simply need a string then it can simply return the string at no cost. So that when you try to print something and you're not getting the expected results, think that your method of printing is wrong - and not the way you store the data. You can get a lot more from AutoIt if you learn to use StringFormat (but this is an ancient secret, only passed down from generation on generation).
Disclaimer: This post contains factual incorrectness and is generally incomplete for the purpose of education.
Edited by Manadar, 28 February 2011 - 01:35 PM.
Posted 28 February 2011 - 02:32 PM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users