You can convert the string into integer like,
1) String = hello ---> Integer = 8,5,12,12,15 (A to Z == 1-26)
2) String = hello ---> Integer = 104,101,108,108,111 (Ascii value)
3) String = HELLO ---> Integer = 72,69,76,76,79 (Ascii value)
[ June 16, 2008: Message edited by: Vishal Pandya ]
Originally posted by Arjun Reddy:
Convert a String say, "Hello" to an integer primitive type?
Java is a very flexible language. you can do all kinds of stuff with it. The difficulty here is that your question doesn't really make a lot of sense. It's kind of like saying "can i convert the string "hello" to a color? or a sound? or a taste?"
YOU have to decide what you mean by converting it to an int. you need to define what is called an 'algorithm' - think of it like a recipe. you would have to define the step by step instructions of what exactly the computer (or a person) should do.
You could convert each letter into some numeric equivalent then sum the total. you could multiply them all together. you could use something more complicated if you wanted a better chance of getting a distinct number for each string.
Then you need to think about case.... is "Hello" treated the same as "HELLO" treated the same as "hello"? What about punctuation?
The difficulty with your post is that the problem is just not well defined. think about these things, and make some decisions. If you then get stuck, come back and ask more questions.
Originally posted by Campbell Ritchie:
You can try using base 36 arithmetic, which allows you to parse any letter, a=10, z=35. It might work with H as well; I have never tried. But you can't use base 37 arithmetic!
Might work, might not.
Thanks Ritchie, fun