Jump to content

Syntax of Binary()


SteveJM
 Share

Recommended Posts

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.

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

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

Link to comment
Share on other sites

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

×
×
  • Create New...