Jump to content

How can I get number from binary


Recommended Posts

This is fairly simple:

$b = Binary("0x...")
$n = Number($b, 3)
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Maybe a bug? For now you can do this:

Func _BinaryToDouble($v)
    Local $t = DllStructCreate("double")
    DllStructSetData(DllStructCreate("byte[8]", DllStructGetPtr($t)), 1, Binary($v))
    Return DllStructGetData($t, 1)
EndFunc

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Not a bug. Number() is defined for values of n such that n is an integer. It says that in the manual. However, what it doesn't say in the manual is that Binary() isn't defined for doubles and floats as well. You're converting a floating point number to binary, hex, or decimal -- so you're going to have to follow IEEE specifications.

It's fun & easy.

Here are some resources:

1). http://en.wikipedia.org/wiki/Floating_point

2). http://www.h-schmidt.net/FloatConverter/IEEE754.html

3). http://sandbox.mc.edu/~bennet/cs110/flt/dtof.html

If nobody writes this, then I'll write something to do it myself. It seems like a good exercise though.

Link to comment
Share on other sites

However, what it doesn't say in the manual is that Binary() isn't defined for doubles and floats as well.

Binary is defined for those values. It returns exactly what it should: the exact binary data stored in memory. It is also documented that AutoIt stores floating point numbers as doubles.

Number on the other hand is not defined for binary input, although the description of the parameter only expects an "expression" It works well with everything except binary data in IEEE 754 format.

If nobody writes this, then I'll write something to do it myself. It seems like a good exercise though.

If you want to write it, don't wait for someone else. The conversion is already possible with the DLLStruct functions. ;) Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Ah... I'm still not sure that Binary() is returning the proper representation of the number. It doesn't look right at all. Let me explain:

F2 = 1111 0010

E4 = 1110 0100

72 = 0111 0010

44 = 0100 0100

DD = 1101 1101

10 = 0001 0000

E4 = 1110 0100

40 = 0100 0000

(Correct my math if I am wrong, I'm doing this in my head.)

SO:

0xF2E47244DD10E440 = 1111 0010 1110 0100 0111 0010 0100 0100 1101 1101 0001 0000 1110 0100 0100 0000

= 64 bits

SO:

The first digit is the sign bit, it is 1 so the number is negative.

1 = -

The next set, from 62 to 52 is the exponent field, and it is:

1110 0101 110

Which is, in decimal

1838 - 1023 = 815

Next up, from 51 to 0, we have the significand, which is:

0100011100100100010011011101000100001110010001000000

In decimal:

0.2778977046384767

Add the magic bit:

1.2778977046384767

Now we have to do the math

2^(Exponent - Bias)*Significand (with magic bit)

And the result of that is:

2.7921744980454277e+245

And because the sign bit is 1, we add a negative:

-2.7921744980454277e+245

^That's the final answer.

Maybe I'm not remembering my assembly correctly; however, I reversed the entire operation and got the same answer back.

-2.7921744980454277e+245

The funny thing is. Reverse the order of the bits, and the answer is, coincidentally, what we want it to be (little endian vs big endian, maybe?)

F2E47244DD10E440 =

40 E4 10 DD 44 72 E4 F2

... Which equals ...

41094.914605567130

Am I an idiot? I dont' know.

I'd love to write this thing in native AutoIt. It's always nice to have stuff run in native code... And it's looking like I need to brush up on my skills. They got a little rusty after not coding in assembly for so long.

Take care,

-- Alex

EDIT: I wanted to add -- I'm going to find my notebook and look up my notes.

Edited by elektron
Link to comment
Share on other sites

(little endian vs big endian, maybe?)

you got that right.

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

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