Jump to content

Convert float to 0's and 1's


Recommended Posts

Here is a code by UEZ

for conversion of Integer to Binary and vice-versa from Post

Code:

#include <String.au3>
Func Integer2Binary($in) ;coded by UEZ
    If $in = 0 Then Return 0
    Local $bin
    While $in > 0
        $bin &= Mod($in, 2)
        $in = Floor($in / 2)
    WEnd
    Return(_StringReverse($bin))
EndFunc
Func Binary2Integer($in) ;coded by UEZ
    Local $int, $x, $i = 1, $aTmp = StringSplit(_StringReverse($in), "")
    For $x = 1 To UBound($aTmp) - 1
        $int += $aTmp[$x] * $i
        $i *= 2
    Next
    $aTmp = 0
    Return StringFormat('%.0f', $int)
EndFunc

I wanted that is it possible to convert a float to binary for example what would be the Binary Version (0's and 1's) of 8.2

Thnx for any help

Regards

Phoenix XL

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

  • Moderators

PhoenixXL,

Take a look here. :)

And good luck as this is a fiendishly complicated area and the cause of many a grey hair! ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

M23,

this is a fiendishly complicated area and the cause of many a grey hair!

Nope I don't think so

Thnx for the link i got the results

for anyoneelse who has the similar problem herez the code

Func _StrBinary($sString)
Local $Binary = ""
For $I = 1 To StringLen($sString)
  $Binary &= _IntBinary(Asc(StringMid($sString, $I, 1)))
Next
Return $Binary
EndFunc
Func _IntBinary($sInteger)
    If $sInteger <= 0 Then Return SetError(1,$sInteger,-1)
    Local $sParameter
    While $sInteger> 0
        $sParameter &= Mod($sInteger, 2)
        $sInteger = _Quotient($sInteger, 2)
WEnd
$sParameter = _StringOpposite($sParameter)
    Return StringFormat("%08s", $sParameter)
EndFunc

Just use the Func _StrBinary()

to convert a float type we can treat it as a string

Regards

Phoenix XL

Edited by PhoenixXL

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

It's rather complex only because our bitwise operations are still mostly limited to 32 bits. Other than that it's straightforward thing:

Func _DoubleToBin($fValue)
    $fValue = Dec(Hex(Number($fValue, 3)))
    Local $bTemp = BinaryMid($fValue, 5, 4)
    Local $sOut
    For $i = 31 To 0 Step -1
        $sOut &= BitAND(BitShift($bTemp, $i), 1)
    Next
    $bTemp = BinaryMid($fValue, 1, 4)
    For $i = 31 To 0 Step -1
        $sOut &= BitAND(BitShift($bTemp, $i), 1)
    Next
    Return $sOut
EndFunc

Few more variations can be written.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

  • Moderators

PhoenixXL,

Nope I don't think so

Wait until you try to do calculations with them - I bet you change your tune then. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

As said, this is quote hard because you can't directly work with such numbers in autoit. This would be my contribution:

ConsoleWrite(DoubleToBinaryString(8.2))


Func DoubleToBinaryString($dIN)
    $sRet = ""
    $sHex = StringSplit(VariableToHexS($dIN),"")
    For $i = 1 to 16
        $sRet &= HexCToBinary("0x" & $sHex[$i]) ;0x needed to correctly get autoit working with hex
    Next
    Return $sRet
EndFunc

Func VariableToHexS($dIn)
    Return Hex($dIn,16)
EndFunc

Func HexCToBinary($hIn)
    $sStr = ""
    For $i = 3 to 0 step -1
        $sStr &= BitAnd(BitShift($hIn,$i),1)
    Next
    Return $sStr
EndFunc

This method can convert any hexstring to binary, so you could use it for strings and any number.

Edited by Shaggi

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Link to comment
Share on other sites

M23,

I bet you change your tune then.

Sorry if the tune got a bit over :)

Take a look here.

Upon taking the look at the previous link posted by M23

i got 8.2=01000001000000110011001100110011

with my Func _StrBinary i too got 8.2=0100000100000110011001100110011

but but but

trancexx with your Func _DoubleToBin I got 8.2 = 0100000000100000011001100110011001100110011001100110011001100110

and Shaggi with your Func DoubleToBinaryString too i got 8.2= 0100000000100000011001100110011001100110011001100110011001100110

Maybe the link given by M23 is giving wrong results so as my Func

M23,

I guess greying of hairs has already started ;)

Edited by PhoenixXL

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

Herez the code i used:

#include <String.au3>
#include <C:UsersabhishekDesktopPresentationAbhishekSCriptsMathsEx.au3>
Func _DoubleToBin($fValue)
    $fValue = Dec(Hex(Number($fValue, 3)))
    Local $bTemp = BinaryMid($fValue, 5, 4)
    Local $sOut
    For $i = 31 To 0 Step -1
        $sOut &= BitAND(BitShift($bTemp, $i), 1)
    Next
    $bTemp = BinaryMid($fValue, 1, 4)
    For $i = 31 To 0 Step -1
        $sOut &= BitAND(BitShift($bTemp, $i), 1)
    Next
    Return $sOut
EndFunc
Func _StrBinary($sString)
Local $Binary = ""
For $I = 1 To StringLen($sString)
  $Binary &= _IntBinary(Asc(StringMid($sString, $I, 1)))
Next
Return $Binary
EndFunc
Func _IntBinary($sInteger)
    If $sInteger <= 0 Then Return SetError(1,$sInteger,-1)
    Local $sParameter
    While $sInteger> 0
        $sParameter &= Mod($sInteger, 2)
        $sInteger = _Quotient($sInteger, 2)
WEnd
$sParameter = _StringOpposite($sParameter)
    Return StringFormat("%08s", $sParameter)
EndFunc
Func _BinInteger($in)
    Local $sInteger, $x, $i = 1, $aTmp = StringSplit(_StringOpposite($in), "")
    For $x = 1 To $aTmp[0]
        $sInteger += $aTmp[$x] * $i*(2^($x-1))
    Next
    $aTmp = 0
    Return StringFormat('%d', $sInteger)
EndFunc
Func _StringOpposite($sString)
Local $sLen=StringLen($sString),$sReturn
For $i=$sLen To 1 Step -1
  $sReturn&=StringMid($sString,$i,1)
Next
Return $sReturn
EndFunc
Func DoubleToBinaryString($dIN)
    $sRet = ""
    $sHex = StringSplit(VariableToHexS($dIN),"")
    For $i = 1 to 16
        $sRet &= HexCToBinary("0x" & $sHex[$i]) ;0x needed to correctly get autoit working with hex
    Next
    Return $sRet
EndFunc
Func VariableToHexS($dIn)
    Return Hex($dIn,16)
EndFunc
Func HexCToBinary($hIn)
    $sStr = ""
    For $i = 3 to 0 step -1
        $sStr &= BitAnd(BitShift($hIn,$i),1)
    Next
    Return $sStr
EndFunc
ConsoleWrite(_StrBinary(8.2)&@CRLF)
ConsoleWrite(_DoubleToBin(8.2)&@CRLF)
ConsoleWrite(DoubleToBinaryString(8.2)&@CRLF)

the above code requires

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

Trancexx' and mine are correct. you can use this for reference:

http://www.binaryconvert.com/result_double.html?decimal=056046050

Edit: seems your first two truncates to single precision.

Edit2: Notice the quote in your link: "The conversion is limited to single precision numbers (32 Bit)". Autoit only uses double-precision floating point numbers, which are 64 bits wide.

Edited by Shaggi

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Link to comment
Share on other sites

Notice the quote in your link: "The conversion is limited to single precision numbers (32 Bit)". Autoit only uses double-precision floating point numbers, which are 64 bits wide.

Thnx Shaggi i didnt noticed that

Thnx to M23 , Tracexx too ;)

Herez the working code i.e. of Trancexx and Shaggi

Func _DoubleToBin($fValue)
    $fValue = Dec(Hex(Number($fValue, 3)))
    Local $bTemp = BinaryMid($fValue, 5, 4)
    Local $sOut
    For $i = 31 To 0 Step -1
        $sOut &= BitAND(BitShift($bTemp, $i), 1)
    Next
    $bTemp = BinaryMid($fValue, 1, 4)
    For $i = 31 To 0 Step -1
        $sOut &= BitAND(BitShift($bTemp, $i), 1)
    Next
    Return $sOut
EndFunc
Func DoubleToBinaryString($dIN)
    $sRet = ""
    $sHex = StringSplit(VariableToHexS($dIN),"")
    For $i = 1 to 16
        $sRet &= HexCToBinary("0x" & $sHex[$i]) ;0x needed to correctly get autoit working with hex
    Next
    Return $sRet
EndFunc
Func VariableToHexS($dIn)
    Return Hex($dIn,16)
EndFunc
Func HexCToBinary($hIn)
    $sStr = ""
    For $i = 3 to 0 step -1
        $sStr &= BitAnd(BitShift($hIn,$i),1)
    Next
    Return $sStr
EndFunc
Edited by PhoenixXL

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

For Debugging i made Float and Double data types using dllstruct create

#include <String.au3>

Func _StrBinary($sString)
Local $Binary = ""
For $I = 1 To StringLen($sString)
  $Binary &= _IntBinary(Asc(StringMid($sString, $I, 1)))
Next
Return $Binary
EndFunc
Func _IntBinary($sInteger)
    If $sInteger <= 0 Then Return SetError(1,$sInteger,-1)
    Local $sParameter
    While $sInteger> 0
        $sParameter &= Mod($sInteger, 2)
        $sInteger = Floor($sInteger/2)
WEnd
$sParameter = _StringOpposite($sParameter)
    Return StringFormat("%08s", $sParameter)
EndFunc
Func _BinInteger($in)
    Local $sInteger, $x, $i = 1, $aTmp = StringSplit(_StringOpposite($in), "")
    For $x = 1 To $aTmp[0]
        $sInteger += $aTmp[$x] * $i*(2^($x-1))
    Next
    $aTmp = 0
    Return StringFormat('%d', $sInteger)
EndFunc
Func _StringOpposite($sString)
Local $sLen=StringLen($sString),$sReturn
For $i=$sLen To 1 Step -1
  $sReturn&=StringMid($sString,$i,1)
Next
Return $sReturn
EndFunc
Func _DoubleToBin($fValue)
    $fValue = Dec(Hex(Number($fValue, 3)))
    Local $bTemp = BinaryMid($fValue, 5, 4)
    Local $sOut
    For $i = 31 To 0 Step -1
        $sOut &= BitAND(BitShift($bTemp, $i), 1)
    Next
    $bTemp = BinaryMid($fValue, 1, 4)
    For $i = 31 To 0 Step -1
        $sOut &= BitAND(BitShift($bTemp, $i), 1)
    Next
    Return $sOut
EndFunc
Func DoubleToBinaryString($dIN)
    $sRet = ""
    $sHex = StringSplit(VariableToHexS($dIN),"")
    For $i = 1 to 16
        $sRet &= HexCToBinary("0x" & $sHex[$i]) ;0x needed to correctly get autoit working with hex
    Next
    Return $sRet
EndFunc
Func VariableToHexS($dIn)
    Return Hex($dIn,16)
EndFunc
Func HexCToBinary($hIn)
    $sStr = ""
    For $i = 3 to 0 step -1
        $sStr &= BitAnd(BitShift($hIn,$i),1)
    Next
    Return $sStr
EndFunc
Local $sStruct=DllStructCreate('float;double'),$sLocale
DllStructSetData($sStruct,1,8.2)
DllStructSetData($sStruct,2,8.2)
$1=DllStructGetData($sStruct,1) ;Float
$2=DllStructGetData($sStruct,2) ;Double
For $x=1 To 2
If $x=1 Then $sLocale='Float'
If $x=2 Then $sLocale='Double'
ConsoleWrite('Func_Name: _StrBinary'&@TAB&@TAB&'    Type:'&$sLocale&'    Value:'&_StrBinary(Eval($x))&@CRLF)
ConsoleWrite('Func_Name: _DoubleToBin'&@TAB&@TAB&'    Type:'&$sLocale&'    Value:'&_DoubleToBin(Eval($x))&@CRLF)
ConsoleWrite('Func_Name: DoubleToBinaryString'&@TAB&'    Type:'&$sLocale&'    Value:'&DoubleToBinaryString(Eval($x))&@CRLF&@CRLF&@CRLF)
Next

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

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