Jump to content

how does binary data work exactly?


Recommended Posts

so, i realize so far i've only messed with string datatypes, in both mirc and autoit all ive messed with are strings. i tried comprehending binary variables in mirc but the tutorial lost me completely. now this isnt an mirc or autoit or any specific language question, its rather a question of how does this datatype work? how do i properly handle this binary data stuff

Link to comment
Share on other sites

Have a look at the Examples in StringToBinary() to get a better understanding.

UDF List:

 
_AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _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() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _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() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Ok, though question, but I'm willing to take a stab. I'm not going to comment on how mIRC handles binary data, because I don't know how that works or even what mIRC means by binary data. So, to answer your question, I have written this long-winded explanation. Did you find any other tutorials that say how binary data is handled? It may be worth it to rewrite this answer and put it on the Wiki under tutorials.

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):

MsgBox(0, "", String($var))

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:

$var = Binary("0x48656C6C6F20576F726C64")
MsgBox(0,"", BinaryToString($var))

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:

$var = Binary("Hello World")
MsgBox(0,"", $var)

This will print: 0x48656C6C6F20576F726C64, but you may get the original message by:

$var = Binary("Hello World")
MsgBox(0,"", BinaryToString($var))

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...