rburello
asked on
ASP Type conversion - from integer to ASCII
I am trying to generate a random string in ASP (not ASP.net). This is a function that I tried to convert from C#, but I am having trouble going from an integer value, and converting it to it's relative ASCII value.....int 122 type cast to a 'z' and then concatinating each random character to a string. These two functions right now give me a random number, but I cannon get the string that I want.
function generate()
dim randomTemp
dim random
for i=0 to 10
randomTemp = randomNumber(122)
CStr(randomTemp)
random = "" & randomTemp
next
generate = random
end function
function randomNumber(intHighestNum ber)
randomize
randomNumber = int(rnd * intHighestNumber) + 1
end function
function generate()
dim randomTemp
dim random
for i=0 to 10
randomTemp = randomNumber(122)
CStr(randomTemp)
random = "" & randomTemp
next
generate = random
end function
function randomNumber(intHighestNum
randomize
randomNumber = int(rnd * intHighestNumber) + 1
end function
ASKER
Still giving me integers. I am not sure if the algorithm has an error, because I keep getting two character int, when I expect a 10 character int. I changed the function from CStr to Chr().
rburello,
function generate()
dim randomTemp
dim random
for i=0 to 10
randomTemp = randomNumber(122)
random = "" & chr(randomTemp)
next
generate = random
end function
function randomNumber(intHighestNum ber)
randomize
randomNumber = int(rnd * intHighestNumber) + 1
end function
Only saw Cstr and didn't spot that it wasn't actually being assigned to anything!
Tim
function generate()
dim randomTemp
dim random
for i=0 to 10
randomTemp = randomNumber(122)
random = "" & chr(randomTemp)
next
generate = random
end function
function randomNumber(intHighestNum
randomize
randomNumber = int(rnd * intHighestNumber) + 1
end function
Only saw Cstr and didn't spot that it wasn't actually being assigned to anything!
Tim
ASKER
OOPS....stupid mistake. Now it is converting to the ASCII character, but I am only getting back a single character now, instead of a 10 character string
Thanks in advance, Tim
Thanks in advance, Tim
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Great !
Thank you, Tim. I'm sorry I made you correct such simple errors for me. Now that that is working, the last part of this function I need is to keep the random numbers in the range of 48 to 122, so it does not put the junk characters into the string (this string will be used in a querystring, by the way).
Thank you, Tim. I'm sorry I made you correct such simple errors for me. Now that that is working, the last part of this function I need is to keep the random numbers in the range of 48 to 122, so it does not put the junk characters into the string (this string will be used in a querystring, by the way).
You need Chr() rather than Cstr()
Cstr(x) converts a numeric datatype to a string datatype e.g. 122 becomes "122"
Chr(x) returns the ascii char from the list index x e.g., Chr(122) = "z"
Tim Cottee