We help IT Professionals succeed at work.

ASP Type conversion - from integer to ASCII

rburello
rburello asked
on
Medium Priority
687 Views
Last Modified: 2010-08-05
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(intHighestNumber)
            randomize
            randomNumber = int(rnd * intHighestNumber) + 1
    end function
Comment
Watch Question

TimCotteeHead of Software Services

Commented:
Hi rburello,

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

Author

Commented:
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().
TimCotteeHead of Software Services

Commented:
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(intHighestNumber)
          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

Author

Commented:
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
Head of Software Services
Commented:
TimCottee,

               random = random & chr(randomTemp)

Not adding the new char each time, missed that as well!

Tim

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
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).
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.