For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > February 2005 > Is there an easy test for String or Hex









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Is there an easy test for String or Hex
SteveC

2005-02-25, 3:55 am

I'm trying to make up a test to see if the first 2 chrs of a string are
a hex number or a string.Its a exercise mainly but I want the first 2
chrs of a password to be a hex number the rest could be numbers or
letters.If it's hex then the test procedes,if it's not it stops and
exits.
thanks
SteveC

Randy Birch

2005-02-25, 3:55 am

With or without the &H prefix? I suspect without, eg you want something like
AE, FF, 0A etc.

If you allow hex values below decimal 10 (A) then how would you qualify 09?
This can be either a valid hex or valid decimal value. If only 10 or above
is allowed, and your only caring that the first two numbers are valid as hex
representations, the lowest value they could type would be 0A, and the
highest FF.

This code fails if the value is 0, or above 255. It treats 01 through 09 as
valid hex entries...


Private Sub Command1_Click()

Dim buff As String

buff = Text1.Text

If IsHex(buff) Then
Print "valid hex: "; buff
Else
Print "invalid entry: "; buff
End If

End Sub


Private Function IsHex(buff As String) As Boolean

On Error GoTo ishex_error

IsHex = CDec("&H" & UCase$(buff)) <> 0

ishex_exit:
Exit Function

ishex_error:
IsHex = False
Resume ishex_exit

End Function



--


Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/


"SteveC" <admlcl@yahoo.com> wrote in message
news:1109300206.769763.86120@o13g2000cwo.googlegroups.com...
: I'm trying to make up a test to see if the first 2 chrs of a string are
: a hex number or a string.Its a exercise mainly but I want the first 2
: chrs of a password to be a hex number the rest could be numbers or
: letters.If it's hex then the test procedes,if it's not it stops and
: exits.
: thanks
: SteveC
:

Rick Rothstein

2005-02-25, 3:55 am

See inline comments...

> With or without the &H prefix? I suspect without, eg you want

something like
> AE, FF, 0A etc.
>
> If you allow hex values below decimal 10 (A) then how would you

qualify 09?
> This can be either a valid hex or valid decimal value. If only 10 or

above
> is allowed, and your only caring that the first two numbers are valid

as hex
> representations, the lowest value they could type would be 0A, and the
> highest FF.


I assumed he wants the first character to be 0-9 or A-F and the same for
the second character.


> This code fails if the value is 0, or above 255. It treats 01 through

09 as
> valid hex entries...
>
>
> Private Sub Command1_Click()
>
> Dim buff As String
>
> buff = Text1.Text
> If IsHex(buff) Then


Shouldn't you be passing only the first two characters in? What if the
Text property has ABX239 (or some such mixture) in it? Yes, I know your
error trap would catch it, but I thought the OP only wanted the first 2
characters to be hex digits, not the entire entry.


> Print "valid hex: "; buff
> Else
> Print "invalid entry: "; buff
> End If
>
> End Sub
>
>
> Private Function IsHex(buff As String) As Boolean
>
> On Error GoTo ishex_error
>
> IsHex = CDec("&H" & UCase$(buff)) <> 0


Why are you using the UCase function (lower case letters are acceptable
to use when forming a VB hex string)? Also, why are you using the CDec
function? Or were you thinking the entire number text entry would be the
hex number (rather than the first 2 characters)?


Rick

Rick Rothstein

2005-02-25, 3:55 am

> I'm trying to make up a test to see if the first 2 chrs of a string
are
> a hex number or a string.Its a exercise mainly but I want the first 2
> chrs of a password to be a hex number the rest could be numbers or
> letters.If it's hex then the test procedes,if it's not it stops and
> exits.


Assuming your TextBox is named Text1...

If Text1.Text Like "[0-9a-fA-F][0-9a-fA-F]*" Then
' First two characters are hex digits
End If

Rick - MVP

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com