Android Question Get ASCII character from number

Discussion in 'Android Questions' started by JakeBullet70, Mar 3, 2015.

  1. JakeBullet70

    JakeBullet70 Well-Known Member Licensed User

    HI all.

    I am trying to get a random PIN number. What I am using is this.

    Code:
    Dim s As StringBuilder : s.Initialize
        s.Append(
    Chr(Rnd(65,132))).Append(Chr(Rnd(65,132))).Append(Chr(Rnd(65,132))).Append(Chr(Rnd(65,132))).Append(Chr(Rnd(65,132)))
        
    Log(s.ToString)
    I see that Chr is looking for UNICODE so how do I get ASCII?
    Thanks
     
  2. sorex

    sorex Expert Licensed User

    that should work, I just don't know why you needed stringbuilder for just concating 4 chars?

    what is the log showing?
     
  3. klaus

    klaus Expert Licensed User

    ASCII stops at Chr(127) !
    Do you really accept characters.
    Isn't a PIN number composed by numbers only ?
    Then you should use Chr(Rnd(48, 58)).
     
    Last edited: Mar 3, 2015
  4. sorex

    sorex Expert Licensed User

    Code:
    Dim pin As String
    pin=getpin
    Log(pin)
    End Sub

    Sub getpin
    Dim p As String
    For x=0 To 3
        
    If Rnd(0,2)=1 Then
            p=p&
    Chr(Rnd(0x41,0x59))
        
    Else
            p=p&
    Chr(Rnd(0x61,0x79))
        
    End If
    Next
    Return p
    End Sub
     
  5. Troberg

    Troberg Well-Known Member Licensed User

    What most people think of as ASCII (for example, the character map used before Unicode in Windows) is actually ANSI (or, rather, a subset of it, which doesn't support stuff like colors and bell), which goes all the way to 255.
     
  6. JakeBullet70

    JakeBullet70 Well-Known Member Licensed User

    Just another case of the stupids... Been having those lately.
    Thanks all.
     
  7. sorex

    sorex Expert Licensed User

    if you want numbers aswell

    Code:
    Sub getpin
    Dim p As String
    For x=0 To 3
        
    Select Case Rnd(0,3)
        
    Case 0:p=p&Chr(Rnd(0x41,0x59))
        
    Case 1:p=p&Chr(Rnd(0x61,0x79))
        
    Case 2:p=p&Chr(Rnd(0x30,0x39))
        
    End Select
    Next
    Return p
    End Sub
    or you could build a string with available chars and use substring to grab a random char.
     
  8. sorex

    sorex Expert Licensed User

    it probably needs to limit to 5A, 7A & 3A or it won't grab the Y,y,9 ;)
     
  9. Troberg

    Troberg Well-Known Member Licensed User

    A little tip that I've used: It's much easier to tell customers their unlock code if it's pronouncible. So, when I made a program to generate such keys a few years ago, I made it so that it alternated vowels and consonants. This also lessened the risk of having keys that contained naughty words somewhat (but not completely).

    Basically, I used two strings, one with vowels, one with consonants, and then alternatingly selected a random character from them (also randomizing which string I started with).

    Also, don't use both upper and lower case letters, and don't mix in numbers. It's simply too much of a human error potential. People forget to press shift, people can't see the difference between 0 (zero) and the letter O and so on. It's better to have a simpler, but longer key.
     
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