Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

what is the importance of converting an int to string and vise versa?

a

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1 icandoit

icandoit

    CC Regular

  • Member
  • PipPipPip
  • 45 posts

Posted 25 March 2013 - 02:06 AM

guyzz...I just can't get the logic of this,why we should convert an int to string or vise versa...what is the importance of this....I just can't get it... thanks in advance...
 



#2 sepp2k

sepp2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 183 posts

Posted 25 March 2013 - 02:26 AM

You'd convert an integer to a string if you want to pass it as an argument to methods that accept a string argument or if you want to call string methods on it. You'd convert a string to an integer if you want to pass it to methods that accept an integer argument or if you want to perform arithmetic on it.



#3 icandoit

icandoit

    CC Regular

  • Member
  • PipPipPip
  • 45 posts

Posted 25 March 2013 - 02:56 AM

You'd convert an integer to a string if you want to pass it as an argument to methods that accept a string argument or if you want to call string methods on it. You'd convert a string to an integer if you want to pass it to methods that accept an integer argument or if you want to perform arithmetic on it.

can you site an example for this....and how can I apply it in real world...
thank you also for your first response.....



#4 Barnsite

Barnsite

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 258 posts

Posted 25 March 2013 - 03:20 AM

A quick example of the difference between using string values vs int values:

 

Assume you had two strings a and b and each were equal to "1".  If you added them together e.g. a + b you would get a string value of "11"

 

If you did the same with int's you would get an int value of 2.


There are two ways to write error-free programs; only the third one works.

#5 KodeKool

KodeKool

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 276 posts

Posted 25 March 2013 - 04:13 AM

Instead of explaining string to int's. a better example (in my opinion anyways) is char to int.

 

Let's say you wanted to generate a random password following a set of rules

 

1: The first character had to be an uppercase letter

2: there has to be at least 1 upper case letter

3: there has to be at least 1 lower case letter

4: there has to be at least 1 number

5: there has to be at least 1 character that is neither a number or a letter

 

the best way to do this quickly is to generate a random number between 1 and 126 (ASCII value equivalents), validate it. and if it's a valid character for the passwords rules. cast it to a character and then add it on to the end of a string variable. if it's not. generate another number.


~A program will always do what you tell it to do, and seldom what you want it to do~

Check out my latest PHP tutorial


#6 icandoit

icandoit

    CC Regular

  • Member
  • PipPipPip
  • 45 posts

Posted 25 March 2013 - 07:04 AM

thanks guyz..it helps a lot..godbless...



#7 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 25 March 2013 - 08:01 AM

Another important case I run into a lot: If the primary key of a database field is a number, say 5, then to get child table records, you would issue the SQL "SELECT * FROM child WHERE foreign_key = 5" as a string, to get the 5 in there, you would need to convert the integer 5 to a string.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#8 KodeKool

KodeKool

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 276 posts

Posted 25 March 2013 - 09:38 AM

Another important case I run into a lot: If the primary key of a database field is a number, say 5, then to get child table records, you would issue the SQL "SELECT * FROM child WHERE foreign_key = 5" as a string, to get the 5 in there, you would need to convert the integer 5 to a string.

 

that's an interesting case. most of the time all my sql statements have automatically done this. so I haven't ever needed to worry about it. what were you coding in when this occured? just so I have an idea where to look out for it :P


~A program will always do what you tell it to do, and seldom what you want it to do~

Check out my latest PHP tutorial


#9 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 25 March 2013 - 12:06 PM

that's an interesting case. most of the time all my sql statements have automatically done this. so I haven't ever needed to worry about it. what were you coding in when this occured? just so I have an idea where to look out for it :P

Usually, this is coded in Delphi, though it would also apply to any case where you don't have a handy .toString() method that calls itself when appropriate :)


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#10 KodeKool

KodeKool

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 276 posts

Posted 25 March 2013 - 12:21 PM

hmm. that's interesting. when I first started incorporating database aspects in my applications i had anticipated this kind of error. however it never occured. eventually I guess I started to assume language developers were clever in that they decided to make that kind of thing a lot easier :P


~A program will always do what you tell it to do, and seldom what you want it to do~

Check out my latest PHP tutorial


#11 Petros

Petros

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 117 posts

Posted 25 March 2013 - 02:06 PM

One more, at least in Java: Integer.valueOf(int).toString().Lenght(). This helps u when you need to perform some kind of process depending on the length of the number.


Java Java Doooooo!!!




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download