Jump to content

how to classify correctly: Int, Float & Binary


Go to solution Solved by jchd,

Recommended Posts

Hi forum,

I try a simple way to classify correctly a number between INT, FLOAT and BINARY, without sucess...

Binary number always return how INT... but is not true.

Someone can help-me here?

Thanks

Local $arr[3] = [1, 1.5, 0x020]

For $each In $arr
    ConsoleWrite('[' & $each & '][' & numType($each) & ']' & @LF)
Next

Func numType($input)
    Select
        Case IsFloat($input)
            Return 'Float'
        Case IsInt($input)
            Return 'Int'
        Case IsBinary(Binary($input))
            Return 'Binary'
    EndSelect
EndFunc   ;==>numType
Edited by detefon

Visit my repository

Link to comment
Share on other sites

Hi,

0x020 is a hexadecimal number, not a binary.

Do it this way :

Local $arr[3] = [1, 1.5, Binary("0x020")]

For $each In $arr
    ConsoleWrite('[' & $each & '][' & numType($each) & ']' & @LF)
Next

Func numType($input)
    Select
        Case IsFloat($input)
            Return 'Float'
        Case IsInt($input)
            Return 'Int'
        Case IsBinary($input)
            Return 'Binary'
    EndSelect
EndFunc   ;==>numType
Edit: Added indents.

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Binary, Hex and integers can all be integers, Binary and Hex are just different ways of presenting the same numbers in a different format.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@FireFox, thanks for your reply.

@BrewManNH, thank you too.

I think I expressed myself poorly and I misunderstood the AutoIt's help...

But put a binary in:

Local $arr[3] = [1, 1.5, Binary("0x020")]

Is not a option, I need identify the tree types of numbers correctly: int, float and HEX.

I study a comunication (two way) from AutoIt and PHP, therefore I thinking is precision type is necessary.

When in AutoIt, detect a hex number ('0x020'), it is classified how 'hex' and send to PHP like 'hex'. The same way to Int and Float.

When AutoIt receive a responde from PHP, this number is classified like hex, and AutoIt transform this data to hex properly.

I am a simple user AutoIt, and I don't have a great experience.
But most important question is: so exactly like that, to know if a number is Int, Float or Hex (the tree formats all numbers), for communication between two different bases (PHP & AutoIt or another) is really necessary?

 

Thanks.

Edited by detefon

Visit my repository

Link to comment
Share on other sites

$HexNumber = "0xFFFF00" ; This is a string, not a number
$Integer = Number($HexNumber) ; this is the string above translated to a number
$Binary = Binary($HexNumber) ; this is the binary representation of the string
ConsoleWrite("$Hexnumber = " & $Hexnumber & @CRLF & "$Integer = " & $Integer & @CRLF & "$Binary = " & $Binary & @CRLF)

In the code above, the top line represents a string that appears to be Hex, but isn't, it's a string. The second line translates that string into a number. If you were to remove the quotes around the string in the first line, the results will change.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Detefon,

As others have already said in substance, you are confusing two distinct notions: the computer/language type of a variable (or a literal value) and its visual representation.

0x20 is the typical representation in hexadecimal (base 16) of the integral value 32 in decimal (base 10) and 00100000 is its representation in base 2.

When displayed or printed, this value can become the strings "0x20" or "32" or "00100000" after conversion.

You and me are commonly representing integers in decimal just because we happen by chance to have 10 fingers.

Should we have only one arm with six fingers, we would probably count in base six, hence 0x20 would be 52 and we would never have imagined a glyph like 6 to represent 10 (in base six).

Beyond visual representation (= nothing more than a convention for display or print), values in a computer are stored according to a type. Every language offers its own set of types to store different things. The string "Defeton" is nowhere close to an integer and 32 is an integral quantity stored as Int just like 0x20.

Floating point values are completely different beasts using a special type to hold [a very small number of] numerical values having an optional fractional part.

Binary type, in the AutoIt context, is a type able to store a length of raw bytes and has nothing to do with representation in base 2.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@jchd, thanky you for you reply, at begining you are correctly... I confuse the thinks, I don't have shame to say this.

But I will learn!

I write this... what do you think?

Local $arr0[1] = ['arrayTest']
Local $arr1[9] = [5, 4.5, 0x2, '0x2', 'hello world', Binary('0x2'), -8, '0x0nothex', $arr0]

For $each In $arr1
    ConsoleWrite('[' & type($each) & '][' & $each & ']' & @LF)
Next

Func type($input)
    If IsArray($input) Then
        Return 'Array '
    Else
        If Number($input) Then
            Select
                Case IsInt($input)
                    Return 'Int   '
                Case IsFloat($input)
                    Return 'Float '
                Case Else
                    Return 'Hex   '
            EndSelect
        Else
            If IsString($input) Then
                Return 'String'
            Else
                Return '??????'
            EndIf
        EndIf
    EndIf
EndFunc   ;==>type

Visit my repository

Link to comment
Share on other sites

  • Solution

Don't be ashamed to have something to learn!

EDIT: this version tests for the Null keyword introduced in the beta. Comment out the relevant part for running under the latest release. (I recommend the beta anyway.)

You can try this for displaying type and contents of a variable or array:

Local $arr0[1] = ['arrayTest']
Local $arr1[9] = [5, 4.5, 0x2, '0x2', 'hello world', Binary('0x2'), -8, '0x0nothex', $arr0]

ConsoleWrite(_VarDump($arr0) & @LF & @LF)
ConsoleWrite(_VarDump($arr1) & @LF)


Func _VarDump(ByRef $vVar, $sIndent = '')
    Local $ret, $len
    Select
        Case IsDllStruct($vVar)
            $len = DllStructGetSize($vVar)
            Local $bstruct = DllStructCreate("byte[" & $len & "]", DllStructGetPtr($vVar))
            $ret = 'Struct(' & $len & ') @:' & Hex(DllStructGetPtr($vVar)) & ' = '
            If $len <= 32 Then
                Return ($ret & DllStructGetData($bstruct, 1))
            Else
                Return ($ret & BinaryMid(DllStructGetData($bstruct, 1), 1, 16) & ' ... ' & StringTrimLeft(BinaryMid(DllStructGetData($bstruct, 1), $len - 15, 16), 2))
            EndIf
        Case IsArray($vVar)
            Local $iSubscripts = UBound($vVar, 0)
            Local $sDims = 'Array'
            $iSubscripts -= 1
            For $i = 0 To $iSubscripts
                $sDims &= '[' & UBound($vVar, $i + 1) & ']'
            Next
            Return $sDims & @CRLF & _VarDumpArray($vVar, $sIndent)
        Case IsBinary($vVar)
            $len = BinaryLen($vVar)
            $ret = 'Binary(' & BinaryLen($vVar) & ') = '
            If $len <= 32 Then
                Return ($ret & $vVar)
            Else
                Return ($ret & BinaryMid($vVar, 1, 16) & ' ... ' & StringTrimLeft(BinaryMid($vVar, $len - 15, 16), 2))
            EndIf
        Case IsBool($vVar)
            Return 'Boolean ' & $vVar
        Case IsFloat($vVar) Or (IsInt($vVar) And VarGetType($vVar) = "Double")
            Return 'Double ' & $vVar
        Case IsHWnd($vVar)
            Return 'HWnd ' & $vVar
        Case IsInt($vVar)
            Return "Integer(" & StringRight(VarGetType($vVar), 2) & ') ' & $vVar
        Case IsKeyword($vVar)
            $ret = 'Keyword '
            If $vVar = Null Then
                Return $ret & 'Null'
            Else
                Return $ret & $vVar
            EndIf
        Case IsPtr($vVar)
            Return 'Pointer @:' & StringTrimLeft($vVar, 2)
        Case IsObj($vVar)
            Return 'Object ' & ObjName($vVar)
        Case IsString($vVar)
            $len = StringLen($vVar)
            $ret = 'String(' & $len & ") "
            If StringRegExp($vVar, "[\x00-\x1F\x7F-\x9F]") Then
                If $len <= 64 Then
                    Return $ret & _DumpStringWithControlChars($vVar)
                Else
                    Return ($ret & StringMid(_DumpStringWithControlChars($vVar), 1, 32) & ' ... ' & StringTrimLeft(StringMid(_DumpStringWithControlChars($vVar), $len - 31, 32), 2))
                EndIf
            Else
                If $len <= 64 Then
                    Return $ret & "'" & $vVar & "'"
                Else
                    Return ($ret & "'" & StringMid($vVar, 1, 32) & ' ... ' & StringTrimLeft(StringMid($vVar, $len - 31, 32), 2)) & "'"
                EndIf
            EndIf
        Case Else
            Return 'Unknown ' & $vVar
    EndSelect
EndFunc   ;==>_VarDump

Func _VarDumpArray(ByRef $aArray, $sIndent = @TAB)
    Local $sDump
    Local $sArrayFetch, $sArrayRead, $bDone
    Local $iSubscripts = UBound($aArray, 0)
    Local $aUBounds[$iSubscripts]
    Local $aCounts[$iSubscripts]
    $iSubscripts -= 1
    For $i = 0 To $iSubscripts
        $aUBounds[$i] = UBound($aArray, $i + 1) - 1
        $aCounts[$i] = 0
    Next
    $sIndent &= @TAB
    While 1
        $bDone = True
        $sArrayFetch = ''
        For $i = 0 To $iSubscripts
            $sArrayFetch &= '[' & $aCounts[$i] & ']'
            If $aCounts[$i] < $aUBounds[$i] Then $bDone = False
        Next

        $sArrayRead = Execute('$aArray' & $sArrayFetch)
        If @error Then
            ExitLoop
        Else
            $sDump &= $sIndent & $sArrayFetch & ' => ' & _VarDump($sArrayRead, $sIndent)
            If Not $bDone Then
                $sDump &= @CRLF
            Else
                Return $sDump
            EndIf
        EndIf

        For $i = $iSubscripts To 0 Step -1
            $aCounts[$i] += 1
            If $aCounts[$i] > $aUBounds[$i] Then
                $aCounts[$i] = 0
            Else
                ExitLoop
            EndIf
        Next
    WEnd
EndFunc   ;==>_VarDumpArray


Func _DumpStringWithControlChars(ByRef $sStr)
    Local $sDump
    Local $aCh = StringToASCIIArray($sStr)
    Local $bInStr = False
    For $ch In $aCh
        Switch $ch
            Case 0 To 0x1F, 0x7F To 0x9F
                If $bInStr Then
                    $bInStr = False
                    $sDump &= "'"
                EndIf
                $sDump &= " 0x" & Hex($ch, 2) & " "
            Case Else
                If Not $bInStr Then
                    $bInStr = True
                    $sDump &= "'"
                EndIf
                $sDump &= ChrW($ch)
        EndSwitch
    Next
    If $bInStr Then $sDump &= "'"
    Return ($sDump)
EndFunc   ;==>_DumpStringWithControlChars
Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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