Android Question Get ASCII character from number

JakeBullet70

Well-Known Member
Licensed User
HI all.

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

B4X:
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
 

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?
 

sorex

Expert
Licensed User
B4X:
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
 

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.
 

JakeBullet70

Well-Known Member
Licensed User
Just another case of the stupids... Been having those lately.
Thanks all.
 

sorex

Expert
Licensed User
if you want numbers aswell

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

sorex

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

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