Sign in to follow this  
Followers 0
SteveJM

Syntax of Binary()

4 posts in this topic

I have been looking at sending/receiving bytes down a serial interface and I got rather confused when using the Binary() command. I did a quick search of the forums without finding any further detail and then thrashed around experimenting, until I think I understand.

In the hope that this might help someone else starting down the same road, here is a trivial bit of code:

$a = Binary(0x12345678)
$b = Binary("ABCDEF")
$c = Binary("0x123456789")
$d = Binary("0x12345678")

MsgBox(0, Default, _
'Binary(0x12345678) gives ' & $a & ", a little-endian integer" & @CRLF & _
'Binary("ABCDEF") gives ' & $b & ", because it's a string. So" & @CRLF & _
'Binary("0x123456789") gives ' & $c & ", also a string, but" & @CRLF & _
'Binary("0x12345678") gives ' & $d & ". Syntax is the same, but now it's treated as Hex." & @CRLF & _
"The rule seems to be that if it can be treated as Hex, it will be!")

With hindsight, it is quite logical and has enough flexibility to cover the most useful cases.

Share this post


Link to post
Share on other sites



It seems that if the Number followed by 0x is 8 in number then it is treated as a Hex..


My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

Here are some results I got

$nBin = BinaryLen('0x')
ConsoleWrite('0 Length: '&$nBin&@CR)
$nBin = BinaryLen('0x1')
ConsoleWrite('01 Length: '&$nBin&@CR)
$nBin = BinaryLen('0x12')
ConsoleWrite('02 Length: '&$nBin&@CR)
$nBin = BinaryLen('0x123')
ConsoleWrite('03 Length: '&$nBin&@CR)
$nBin = BinaryLen('0x1234')
ConsoleWrite('04 Length: '&$nBin&@CR)
$nBin = BinaryLen('0x1235')
ConsoleWrite('05 Length: '&$nBin&@CR)
$nBin = BinaryLen('0x12356')
ConsoleWrite('06 Length: '&$nBin&@CR)
$nBin = BinaryLen('0x1342567')
ConsoleWrite('07 Length: '&$nBin&@CR)
$nBin = BinaryLen('0x12345678')
ConsoleWrite('08 Length: '&$nBin&@CR)
$nBin = BinaryLen('0x098765432')
ConsoleWrite('09 Length: '&$nBin&@CR)
$nBin = BinaryLen('0x1234567890')
ConsoleWrite('10 Length: '&$nBin&@CR)
$nBin = BinaryLen('0x09876543212')
ConsoleWrite('11 Length: '&$nBin&@CR)
$nBin = BinaryLen('0x123456789012')
ConsoleWrite('12 Length: '&$nBin&@CR)

#cs
Hence we conclude that on every Even Number it is treated as a Hex and Hence the Binary Length is Half its Length,
and on Every Prime Number we get the total number of strings + 2 because of 0x.
#ce

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hi,

It's called BinaryLen but it can calc the length of every base from 0 to 16, and even if the "Binary" is invalid, it returns the length of the specified string.

"0x" is used to specify that it's hexadecimal, so "0x" is not a binary that's why you get 0.

Hope I'm right.

Edit : Be sure that what you pass as a parameter is in Binary type, otherwise the length won't be correct.

Br, FireFox.

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Similar Content

    • Dragonfighter
      By Dragonfighter
      I'm searching a way to do xor and shift and if possible also other operations. Thanks in advance for the replies.
    • rudi
      By rudi
      Hello.
      I'm too stupid to see my mistake:
      To investigate the internal "dictionary" of TIFF files I'd like to read in the files in binary mode and to check, if there are more than one pages "in" this TIFF.
      Notepad++, "View as Hex" is presenting the first bytes as "49 49 2a 20 08 20 20 20 12" for the TIF attached to this posting
      The "TIFF Header Format" is easy:
      Offset 00h, 2 Byte = Byte Order, "II"=intel, "MM"=motorola. (I = 0x49)
      --> II
      Offset 02h, 2 Byte = Version Nr.
      Offset 04h, 4 Byte = pointer to first IFD entry
      Description of TIFF header: https://www.awaresystems.be/imaging/tiff/faq.html#q3
       

      Howto read and analyse the binary content correctly? This is my messy, not operational code:
       
      $sampleTiff="H:\daten\tif\11\11\111111.TIF" $h=FileOpen($sampleTiff,16) $content=FileRead($h) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $content = ' & $content & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console FileClose($h) $type=VarGetType($content) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $type = ' & $type & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $ToString=BinaryToString($content) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $ToString = ' & $ToString & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ConsoleWrite(@CRLF & @CRLF) $content=StringTrimLeft($content,2) ; cut off the leading "0x" ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $content = ' & $content & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console for $i = 1 to 8 step 8 $next=StringMid($content,$i,2) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $next = ' & $next & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $Chr=BinaryToString($next) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Chr = ' & $Chr & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ConsoleWrite(@CRLF & "---" & @CRLF) Next Regards, Rudi.
      111111.TIF
    • ur
      By ur
      When I am trying to compile the autoit files with aut2exe.
      I am getting below error.
      There is no issue in code as the same code is getting compiled on different machine.
      I tried reinstalling the AUtoIT, but the issue replicates.

      Any suggestions?
    • wakillon
      By wakillon
      BinaryToAu3Kompressor v1.0.5.4
       

       
      It's now possible to see the best compression ratio using LZMA, LZNT and Base64 compressions with differents combinations.
      Nothing too complicate, you drag'n drop a file on the picture and script Test all compression types and return the ratios.
      ( Test duration depends of file size, slowest compression is LZNT, but all decompressions are fast  )
      Free to you after, to choose the compression(s) you want...
      Yes, LZMA needs a dll ( embedded & compressed in script ) but brings a powerfull compression. 
      It opens scite with your file compressed to an au3 script with or without decompression function as you want.
      Hold Left Shift key when clicking button for just copy script to clipboard.
      Use the 3 compressions at a time works but doesn't give a good ratio, that's why i don't display it.
      Usefull for little files you want include in your scripts !
      No externals files needed, they are already in script.
      Previous downloads : 1103
      Source and Executable
      BinaryToAu3Kompressor will be added to the next version of >SciTEHopper
      Thanks to Ward for his >Base64.au3 and LZMA.au3, and trancexx for his >LZNT functions and his >Base64Decode function.
    • WoodGrain
      By WoodGrain
      Hi All,
      Trying to convert a number to binary zeros and ones but I'm getting a result I don't understand and looks more like hex than binary.
      Here's my basic code:
      $myNum = 11 $myNumBin = Binary($myNum) MsgBox(0, "Binary result", $myNumBin) What I want is "1011", what I get is 0x0B000000.
      Thanks!