Announcement

Collapse
No announcement yet.

Convert string to int - How?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Convert string to int - How?

    I need to be able to convert a string variable into an int. e.g. "123" would have the value 123.

    But I can't see any way of doing this in the documentation (WOTgreal Help).

    Am I missing something???

    Thanks for any help

    #2
    Almost all primitive data types can be casted to each other. It's pretty basic for most languages. No offense It's also on the wiki.

    int("123") will cast to an int with the value 123. There's also float(), string(), byte(), class<MyClass>(class'MySuperClass'), char(), Asc(), plus your object casts, heh.

    Anyway, I'm sure that helps.

    Comment


      #3
      Thanks for that

      I'm actually a professional programmer in C#, Java and C++ so I know about casting The thing is I tried casting, but I just got the syntax wrong Casting is different in the other languages. My excuse and I'm sticking with it

      Anyway, thanks again.

      Comment


        #4
        Good deal. Try the wiki for this stuff, most syntax issues are addressed. If youre coming from c++/c# you may need it. http://wiki.beyondunreal.com/wiki

        I believe the syntax is similar to java, if I recall. Been a while since I've used java (didnt really find it useful).

        Comment


          #5
          Syntax is a little strange for me at the moment, so I'll definitely go look at the wiki.

          UnrealScript is very similar to Java as you say, but casting is the same in Java as it is in C++. To cast a float to an int is (int)variable for example, which is what I tried. But Java is completely different when trying to convert a string to a int. You need to use Integer.parse("123").

          Thanks for the help though - I'll go read the wiki now

          Comment


            #6
            casting in UnrealScript is more like casting in Pascal

            (and I prefer the Pascal method of casting)

            Comment


              #7
              Wiki will tell you up and down that typecasting INT and STRINGS isn't possible. And, well it isn't, not thru normal typecasting. Maybe there is an existing function or you could make one that prys the string apart for numbers. However, since a string could, obviously, contain letters and symbols, you cannot typecast from INT to STRING or vise versa. Tried it, and it's written that it's "impossible". Only makes sense. (once again I'll say, "by normal means" hehe)

              Comment


                #8
                Originally posted by PlayerPawn
                Wiki will tell you up and down that typecasting INT and STRINGS isn't possible. And, well it isn't, not thru normal typecasting. Maybe there is an existing function or you could make one that prys the string apart for numbers. However, since a string could, obviously, contain letters and symbols, you cannot typecast from INT to STRING or vise versa. Tried it, and it's written that it's "impossible". Only makes sense. (once again I'll say, "by normal means" hehe)
                wtf are you talking about

                the following is perfectly legal
                Code:
                var int i;
                var string s;
                
                s = string(i);
                i = int(s);

                Comment


                  #9
                  Right, lol.

                  Seriously, its absolutely possible, as many a programmer has done it for many uses. Especially int to string, lol. The cast from string to int will fail if there are characters, but I'm pretty sure a - will work.

                  Infact, for a cheat protection I maintain for UT99, I have a string encryption method that uses string to int conversions when it decrypts the strings. This just wouldnt be possible if it didnt work, heh.

                  Comment


                    #10
                    Agreed, string to int is as simple as int("123") as I am now successfully using this method.

                    Comment


                      #11
                      My wording was off, but your orginal question was String to Int. I'm new to coding so sorry, but I read two documents that said that isn't possible. I'll use your stated example if I ever need to do it myself. Goes you show I can't use others documents as facts. Thanks, crew.

                      Comment


                        #12
                        Im getting ready to figure out how to convert a cache string to an array, if its even possible. The variable I want to change is map prefix so I can load any type of map from my gametype. Anyone done this already or know an easy way?

                        Im just starting to get deep in the code so bear with me.

                        Comment


                          #13
                          Map prefixes are just strings. Cache functions call the maps up by prefix, so youll just have to load them all. Er, I think thats what you're asking, lol.

                          Comment


                            #14
                            so if I want to be able to load every map in my maps folder with my custom gametype what should I put for the map prefix string in my gameinfo? or is there a function I need to override?

                            Comment


                              #15
                              The prefix might be optional, which would mean that all maps will load if the prefix is left blank. But then again, if its not optional, youll probably have to call the function for each prefix, and store them in seperate arrays. I'm not sure if the code empties the input array before it fills it or not.

                              Comment

                              Working...
                              X