iOS Question IsNumber to work on hexstrings?

Discussion in 'iOS Questions' started by techknight, Apr 14, 2015.

  1. techknight

    techknight Well-Known Member Licensed User

    Is there a way to test hexstrings to see if its a valid hex value?

    For example my packet always comes in as a hex string. it has "123CXXXXX" 3C being the length.

    Problem is, if for some odd reason my Socket gets something other than a hex string, it crashes because well, 3C isnt a length, its whatever the text is. Such as any combination of letters and numbers.

    Is there a way to test that the string has all valid hexadecimal letters/numbers?
     
  2. omidaghakhani1368

    omidaghakhani1368 Well-Known Member Licensed User

    Hello my friend
    I suggest use regex.
    Code:
    Log(Regex.IsMatch("^(0x|0X)?[a-fA-F0-9]+$",yourhexstring))
    yourhexstring is hex number
    If match return true
     
    Erel likes this.
  3. techknight

    techknight Well-Known Member Licensed User

    Ah. Still from the VB6 world so I didnt know anything about a Regex.

    Plus what do all the random characters you have listed mean? I am sure they arnt random, but I see alot of extras that I dont know what they are doing there?
     
  4. omidaghakhani1368

    omidaghakhani1368 Well-Known Member Licensed User

    Oh my god,Regex is sweetest topic in programming world.
    Regex is complete and without any problem and too complex
     
  5. techknight

    techknight Well-Known Member Licensed User

    Well I dont program in any other language except BASIC, and regex doesnt really exist in BASIC except for maybe this. And I came from VB6 and it doesnt have Regex
     
  6. omidaghakhani1368

    omidaghakhani1368 Well-Known Member Licensed User

    Regex exist in all programming language
    Regex in basic is easy.
    Use my example code in above post
     
  7. techknight

    techknight Well-Known Member Licensed User

    Huh. Learn something new everyday, I never used it in VB6, at least I dont recall.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice