Jump to content

BinaryString() bytes in reverse order?


Recommended Posts

I'm working on optimizing a base64 conversion routine and I go the idea that I can use a BinaryString() function call instead of doing lots of Chr() functions. I was also hoping that I could use the int() and/or Number() functions instead of asc() functions.

This code:

$i = ((65 * 256 + 66) * 256 + 67) * 256 + 68
$b = BinaryString($i)
$n = Number($b)
ConsoleWrite($i & @CR)
ConsoleWrite($b & @CR)
ConsoleWrite($n & @CR)

Outputs this:

1094861636
DCBA
68

(FYI: Chr(65)='A')

First, I notice that BinaryString() puts the bytes in reverse order. This isn't a bug, as this is the way that the function is described in the manual. I'm just curious as to why it would do this - what's the purpose of reversing the bytes?

Second, why does the Number() function only consider the first byte of the BinaryString? The statment "Number(string(256*256*256*256))" works as expected, so obviously the Number() function is capable to handle numbers bigger than 8 bits?

And one more thing - BinaryString() always returns a string that is a multiple of 4. So the statement "StringLen(BinaryString(65))" evaluates to 4. Why is this? Agagin, this is not a bug, it is in the manual, but I just want to know why.

Edited by blindwig
Link to comment
Share on other sites

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