Jump to content

BinaryToNumber


Recommended Posts

Hey Does anyone have a Binary to number function? the binarytostring function does not return a number

I have $b = Binary(256)

i need to retrieve the 256 later.

Heres my current non-working test code

$b = binary(256)
MsgBox(0,"",$B)
$b = BinaryToString($B)
MsgBox(0,"",$B)
$b = asc($B)
MsgBox(0,"",$B)

Thanks in advanced

[size="10"]Pure Au3 crypt funcs(I'm currently also working on making a dll from this)[/size][Y] Be more active in the community[Y] Get 200 posts[N] Get 300 posts[N] Make a Topic in the example scripts forum with at least 50 replies.People who currently hate me:ValikSmOke_N

Link to comment
Share on other sites

Here's an exmaple script on how to do it.

http://www.autoitscript.com/forum/lofivers...php?t70507.html

I cut out the section of code below that should be all you need.

I tested this myself and it worked great.

-Kenny

#include <String.au3>

$BinaryString = InputBox("Test", "Enter Binary Number to Convert")
$Converted = _BinaryToDec($BinaryString)
MsgBox(4096,"Test", "Converted Number: " & $Converted)

Func _BinaryToDec($strBin)
Local $Return
Local $lngResult
Local $intIndex

If StringRegExp($strBin,'[0-1]') then
$lngResult = 0
  For $intIndex = StringLen($strBin) to 1 step -1
    $strDigit = StringMid($strBin, $intIndex, 1)
    Select 
      case $strDigit="0"
     ; do nothing
      case $strDigit="1"
        $lngResult = $lngResult + (2 ^ (StringLen($strBin)-$intIndex))
      case else
     ; invalid binary digit, so the whole thing is invalid
        $lngResult = 0
        $intIndex = 0; stop the loop
    EndSelect
  Next

  $Return = $lngResult
    Return $Return
Else
    MsgBox(0,"Error","Wrong input, Conversion Failed, try again ...")
    Exit
    Return
EndIf   
EndFunc
Edited by ken82m

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

Hey Does anyone have a Binary to number function? the binarytostring function does not return a number

I have $b = Binary(256)

i need to retrieve the 256 later.

Heres my current non-working test code

$b = binary(256)
MsgBox(0,"",$B)
$b = BinaryToString($B)
MsgBox(0,"",$B)
$b = asc($B)
MsgBox(0,"",$B)oÝ÷ Ù8ZK"§ojwwú®¢×+,y鬶(®F®¶­sbb33c¶"Ò&æ'gV÷C³gV÷C²fײW#Sb¤×6t&÷ÂgV÷C²gV÷C²Âb33c¶" ¢b33c¶"ÒFV2Wb33c¶"¤×6t&÷ÂgV÷C²gV÷C²Âb33c¶"
Link to comment
Share on other sites

Nethire of yours works. Both return 0

Ken82n's sort of works but it wont work with a 0x infront of the binary(autoit style)

And number doesnt work for numbers higher than 255

edit:

@malkey i need it to be [psuedocode]binary(number)[/psuedocode] and thats all for declasing $b initially

Edited by mmavipc

[size="10"]Pure Au3 crypt funcs(I'm currently also working on making a dll from this)[/size][Y] Be more active in the community[Y] Get 200 posts[N] Get 300 posts[N] Make a Topic in the example scripts forum with at least 50 replies.People who currently hate me:ValikSmOke_N

Link to comment
Share on other sites

It worked ok for me. Give me the number your trying to convert and the result your expect.

Thanks,

Kenny

Nethire of yours works. Both return 0

Ken82n's sort of works but it wont work with a 0x infront of the binary(autoit style)

And number doesnt work for numbers higher than 255

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

It worked ok for me. Give me the number your trying to convert and the result your expect.

Thanks,

Kenny

binary: 0x00010000

expected: 256

edit: @Richard:

the test code I used for your soulution was

$b = binary(256)
MsgBox(0,"",$B)
;~ $b = BinaryToString($B)
;~ MsgBox(0,"",$B)
$b = Number($B)
MsgBox(0,"",$B)
msgbox(0,";dfk",Number(binary(256)))
try it yourself

edit @malkey:

The reason that I need it to be binary(number) is becuase the binary is generated by tcprecv.

Edited by mmavipc

[size="10"]Pure Au3 crypt funcs(I'm currently also working on making a dll from this)[/size][Y] Be more active in the community[Y] Get 200 posts[N] Get 300 posts[N] Make a Topic in the example scripts forum with at least 50 replies.People who currently hate me:ValikSmOke_N

Link to comment
Share on other sites

hmmm your a little beyond me I'm afraid. According to the online converter I'm using 256 = 100000000

Do you have the TCPRECV command flagged with 1 to force binary?

Edited by ken82m

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

hmmm your a little beyond me I'm afraid. According to the online converter I'm using 256 = 100000000

Thats regular binary not au3 binary

[size="10"]Pure Au3 crypt funcs(I'm currently also working on making a dll from this)[/size][Y] Be more active in the community[Y] Get 200 posts[N] Get 300 posts[N] Make a Topic in the example scripts forum with at least 50 replies.People who currently hate me:ValikSmOke_N

Link to comment
Share on other sites

What the heck is au3 binary?

P.S. You wrote 0x, which is the prefix for hex, isnt it? But that number following it is 65536 (decimal)

That's what I was thinking but he's right binary(256) returns the 0x number he provided.

But i can't find any forumla using hex or bin to get it back to 256.

I can't find anyway to get any of these conversions autoit is doing to match up to any math I've learned....

Edited by ken82m

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

Hi mmavipc,

Please create descriptive topics.

Using one word topics makes it really difficult to use the search feature, and those that could probably answer your question will probably not even read your thread.

Doing it enough could cause your posting privileges to be taken away all together, and we don't want that :P .

Thanks.

Link to comment
Share on other sites

Try this one.

Local $b = Binary(256)
MsgBox(0, "", $B)

Local $a = Bin2Num($B)
MsgBox(0, "", $a)

Func Bin2Num($4Bytes)
    Local $dllStruct2_Integer = DllStructCreate("int")
    Local $dllStruct2_Binary = DllStructCreate("byte[4]", DllStructGetPtr($dllStruct2_Integer))

    DllStructSetData($dllStruct2_Binary, 1, $4Bytes)
    Return DllStructGetData($dllStruct2_Integer, 1)
EndFunc   ;==>Bin2Num
Edited by Malkey
Link to comment
Share on other sites

Nice! I was trying to find some commands relating to bytes just as an idea earlier but found nothing.

I damn well wouldn't have come up with that one.

Something similiar should be added as a built in function.

Good one for the Example forum if it's not already there.

-Kenny

Try this one.

Local $b = Binary(256)
MsgBox(0, "", $B)

Local $a = Bin2Num($B)
MsgBox(0, "", $a)

Func Bin2Num($4Bytes)
    Local $dllStruct2_Integer = DllStructCreate("int")
    Local $dllStruct2_Binary = DllStructCreate("byte[4]", DllStructGetPtr($dllStruct2_Integer))

    DllStructSetData($dllStruct2_Binary, 1, $4Bytes)
    Return DllStructGetData($dllStruct2_Integer, 1)
EndFunc   ;==>Bin2Num
Edited by ken82m

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

Try this one.

Local $b = Binary(256)
MsgBox(0, "", $B)

Local $a = Bin2Num($B)
MsgBox(0, "", $a)

Func Bin2Num($4Bytes)
    Local $dllStruct2_Integer = DllStructCreate("int")
    Local $dllStruct2_Binary = DllStructCreate("byte[4]", DllStructGetPtr($dllStruct2_Integer))

    DllStructSetData($dllStruct2_Binary, 1, $4Bytes)
    Return DllStructGetData($dllStruct2_Integer, 1)
EndFunc   ;==>Bin2Num
THANK YOU SOO MUCH

you are awsome :P

[size="10"]Pure Au3 crypt funcs(I'm currently also working on making a dll from this)[/size][Y] Be more active in the community[Y] Get 200 posts[N] Get 300 posts[N] Make a Topic in the example scripts forum with at least 50 replies.People who currently hate me:ValikSmOke_N

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