• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 672
  • Last Modified:

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(intHighestNumber)
            randomize
            randomNumber = int(rnd * intHighestNumber) + 1
    end function
0
rburello
Asked:
rburello
  • 3
  • 3
1 Solution
 
TimCotteeCommented:
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
0
 
rburelloAuthor 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().
0
 
TimCotteeCommented:
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
rburelloAuthor 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
0
 
TimCotteeCommented:
TimCottee,

               random = random & chr(randomTemp)

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

Tim
0
 
rburelloAuthor 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).
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now