Jump to content

Help IsBinary Or IsString Or IsNumber


Trong
 Share

Go to solution Solved by kylomas,

Recommended Posts

;~ IsBinary
Local $sStr="0xFF" ;0xFF
If IsBinary($sStr) Then
    ConsoleWrite($sStr&" is a Binary" &@CRLF)
Else
    ConsoleWrite($sStr&" is not a Binary"&@CRLF)
EndIf


;~ IsNumber
Local $sStr=0x12 ;"0x12"
If IsNumber($sStr) Then
    ConsoleWrite($sStr&" is a number" &@CRLF)
Else
    ConsoleWrite($sStr&" is not a number"&@CRLF)
EndIf


;~ IsString
Local $sStr="938"
If IsString($sStr) Then
    ConsoleWrite($sStr&" is a String" &@CRLF)
Else
    ConsoleWrite($sStr&" is not a String"&@CRLF)
EndIf


;~ ///////////////////////////////////////////////////////

Local $sStr="9H38"
If Number($sStr)<>0 Then
    ConsoleWrite($sStr&" is a number" &@CRLF)
Else
    ConsoleWrite($sStr&" is not a number"&@CRLF)
EndIf
0xFF is not a Binary
18 is a number
938 is a String
9H38 is a number

I want to check the "Var" is a number or a string or is Binary.

If var is "0xFF", it is Binary.

If var is "938", it is Number.

If var is "9H38", it is String.

How it works correctly?

 

Edited by Trong

Regards,
 

Link to comment
Share on other sites

Check out VarGetType in the Help file...

edit: additional info 

If your value is in quotes it is a string.  There are a number of StringIs* functions to detect what the string might be representing.  See the Help file.

When you do Number($var) then the result is always a number...

There is a very specific set of rules for how different data representations are treatred, again, see the Help file.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

  • Solution

Run this...

Local $aStr = "123"
ConsoleWrite("Is" & VarGetType($aStr) & " variable type." & @CRLF)

ConsoleWrite( (stringisdigit($aStr) ? 'Yes, it is a digit' : 'No, it is Klingon') & @CRLF)

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Here's a function I wrote a while ago. Maybe it can be useful.

;

Local $aNumber[7] = ["12","5.6","7e-7","0xF","1.4e+130","0x12345677890ABC","1+1"]

For $i = 0 To 6
    ConsoleWrite(_StringIsNumber($aNumber[$i]) & @LF)
Next

; #FUNCTION# ====================================================================================================================
; Name...........: _StringIsNumber
; Description ...: Checks whether a string is a number as recognised by the AutoIt interpreter
; Syntax.........: _StringIsNumber($sString [, $bVulgarFrac])
; Parameters ....: $sString     - The string to test
;                  $bVulgarFrac - [Optional] if set to True, vulgar fractions will also return True
; Return values .: True or False
; Author ........: czardas
; Remarks .......: Returns True for integers, floats, hexadecimal and scientific notation.
; Related .......: StringIsDigit, StringIsFloat, StringIsInt, StringIsXDigit
; Link ..........:
; Example .......: MsgBox(0, "1.2e-300 is a number", _StringIsNumber("1.2e-300"))
; ===============================================================================================================================

Func _StringIsNumber($sString, $bVulgarFrac = False)
    Local $bReturn = False
    If StringIsInt($sString) Or StringIsFloat($sString) Then
        $bReturn = True ; string is integer or float
    ElseIf StringRegExp($sString, "(?i)(\A[\+\-]?0x[A-F\d]+\z)") Then
        $bReturn = True ; string is hexadecimal integer
    ElseIf StringRegExp($sString, "(?i)(\A[\+\-]?\d*\.?\d+e[\+\-]?\d+\z)") Then
        $bReturn = True ; exponential (or scientific notation)
    ElseIf $bVulgarFrac And StringRegExp($sString, "(\A[\+\-]?\d+/\d+\z)") Then
        $bReturn = True ; string is a vulgar fraction
    EndIf
    Return $bReturn
EndFunc ; _StringIsNumber
Link to comment
Share on other sites

Another version but it doesn't accept the positive sign (as in +1): which is seen as an expression. This can be changed.

Func _StringIsNumber($sString)
    Return StringRegExp($sString, "(?i)(\A)(\-?\d+(\.\d+)?(e[\+\-]\d{1,3})?|0x[A-F\d]{1,16})(\z)") = 1
EndFunc

;

Edit: Updated to reject binary (0x7FFF etc) with more than 16 hex digits (Max Int64 value).

BTW thanks for voting in the Wikipedia Example Challenge. :thumbsup:

Edited by czardas
Link to comment
Share on other sites

First I nested these particular expressions in parentheses and it didn't work. I then thought it would be more consistant to create them as separate groups and it worked. However, you are right to point out that it is needless bloat. Thanks!

Func _StringIsNumber($sString)
    Return StringRegExp($sString, "(?i)\A(\-?\d+(\.\d+)?(e[\+\-]\d{1,3})?|0x[A-F\d]{1,16})\z") = 1
EndFunc
Edited by czardas
Link to comment
Share on other sites

 

First I nested these particular expressions in parentheses and it didn't work. I then thought it would be more consistant to create them as separate groups and it worked. However, you are right to point out that it is needless bloat. Thanks!

Func _StringIsNumber($sString)
    Return StringRegExp($sString, "(?i)\A(\-?\d+(\.\d+)?(e[\+\-]\d{1,3})?|0x[A-F\d]{1,16})\z") = 1
EndFunc

What is the best?

Local $STR="0xFF"
MsgBox(0,StringIsDigit($STR),_StringIsNumber($STR))

Func _StringIsNumber($sString)
    Return StringRegExp($sString, "(?i)\A(\-?\d+(\.\d+)?(e[\+\-]\d{1,3})?|0x[A-F\d]{1,16})\z") = 1
EndFunc

Regards,
 

Link to comment
Share on other sites

It depends on what you want to do with the result. The function above was created to ensure that the string could safely be converted to a number using the function Execute(). I also used an additional test to check the value remains within range, so that it can be handled by the interpreter (not posted here). If your application does not use floats, or exponential notation, then a more specific test will be more suitable. If you have a clear definition of exactly which formats a number should take in your program, then you can write the code to check those cases only.

0xFF is seen as a number by the interpreter, but the string variant "0xFF" is not seen as a number. StringIsDigit() will only test for characters in the range 0-9, so many types of numeric formats will be rejected. IsNumber() does not detect if a string might represent a number or not. Using Number() to first convert a string to a number is not always a reliable solution - for example the boolean value True will be converted to 1 which is a number.

;

MsgBox(0, "Convert True", Number(True))

;

So it is a question of having a complete overview of all the numbers you need to test, and only then should you search for the best solution. You should always try things out to get a clearer understanding of your design choices. I hope some of that made sense. :)

Edited by czardas
Link to comment
Share on other sites

It depends on what you want to do with the result. The function above was created to ensure that the string could safely be converted to a number using the function Execute(). I also used an additional test to check the value remains within range, so that it can be handled by the interpreter (not posted here). If your application does not use floats, or exponential notation, then a more specific test will be more suitable. If you have a clear definition of exactly which formats a number should take in your program, then you can write the code to check those cases only.

 

0xFF is seen as a number by the interpreter, but the string variant "0xFF" is not seen as a number. StringIsDigit() will only test for characters in the range 0-9, so many types of numeric formats will be rejected. IsNumber() does not detect if a string might represent a number or not. Using Number() to first convert a string to a number is not always a reliable solution - for example the boolean value True will be converted to 1 which is a number.

 

So it is a question of having a complete overview of all the numbers you need to test, and only then should you search for the best solution. You should always try things out to get a clearer understanding of your design choices. I hope some of that made sense. :)

Thank you very much!

If IsNumber Then $it=Number($it)

If IsString Then $it="$it"

If IsBinary Then $it=BinaryToString($it)

Regards,
 

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