converting string to int in java

  • Thread starter Thread starter GeX
  • Start date Start date

GeX

GeX

Soldato
Joined
17 Dec 2002
Posts
7,030
Location
Manchester
hi. can anyone help me out with this?

i want to convert a string to an int, specifics of the int is not important. It does not have to be the ascii equivilant of it. I'm not sure the best way to approaching this.

Am working in Java, would it make sense to declare the string as an array of char and then loop through, pulling each char out and adding its ascii equivilant int to a running total?

How would i go about coding that. I'm a tad rusty!

edit: the outcome i want from this, is a number generated based on the string. that's all.
 
Last edited:
thanks guys, the reason i suggested doing as i did is because that is how i can work through it in my head.

Integer.parseInt("10"); <-- much simpler, but i'm not sure how to use that. Can you put it in some context?
 
no, that wouldn't work. i won't have ints in the string, it'll be actual characters.

basically i want to convert abcdefgh to an int
 
i want to generate a unique number based on the string, maybe convert each character to its ascii value and then add them together. hence my slightly odd way of looking at it when i first posted!

the hashCode of any object contains an int i think.

Code:
String str = "hello";
int i = str.hashCode();
System.out.println(i);

thats something i didn't think of, cheers. looks like that;ll do what i want, ta!
 
Last edited:
that's even better, thanks for that. it 'fits' better because it's how i was thinking it would work. ta. :)
 
it would be better, yeah.

could use both methods, using the hashcode to convert the int to a unique number.
 
Last edited:
Back
Top Bottom