my dog learned polymorphism*
The moose likes Beginning Java and the fly likes String to int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String to int" Watch "String to int" New topic
Author

String to int

adi bashir
Greenhorn

Joined: Feb 06, 2012
Posts: 9
hi everyboby. can you please tell me how to convert string like "adil" to an integer type?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

The Integer class has a couple of parseInt() methods.

What integer value would you expect the String "adil" to have, and why? That is, what conversion rules are you planning to use?
adi bashir
Greenhorn

Joined: Feb 06, 2012
Posts: 9
Actually i have to use this conversion in RSA algorithm, in which the user enters a message which is to be encrypted, but i have devised the method for integer type messages and not string type, so i need to perform the conversion?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11246
    
  16

An example is not a spec. it is incomplete at best, most often vague, and in some cases (like this one), completely unhelpful. If i said "What is the next number in this sequence: 1 2", an argument could be made for 3 or 4 being next - both are valid, since there isn't enough data provided.

There is no logical, obvious way to convert "adil" into an integer type. I can think of 4-5 possible algorithms, each giving a different answer.

So, tell us EXACTLY what you want to do - how would YOU convert "adil" into an integer type, if all you had was paper and pencil?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
adi bashir
Greenhorn

Joined: Feb 06, 2012
Posts: 9
for example i want to convert string "adil" to its own calculated integer value or i can say ASCII value like 56?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

If you want to implement the RSA algorithm yourself, then just get all the bytes from the String. Look at the docs for the String class for a method to do that. Then operate on those bytes according to the rules of the algorithm.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

adi bashir wrote:for example i want to convert string "adil" to its own calculated integer value


Do you honestly think that adds any useful information? Anyway, see my previous post about the String's bytes.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11246
    
  16

adi bashir wrote:for example i want to convert string "adil" to its own calculated integer value or i can say ASCII value like 56?

You can do whatever you want. That's the problem...we don't know what you want. Where does '56' come from?

We don't know what you are trying to do. You could:

1) Convert any 'a' to 1, 'b', to 2, 'c' to 3...and add them up, getting one single number
2) Use the ascii value of each letter, square it, find the mean, then take the square root
3) count the number of letters in the string and return that
4) create a map that says "adil" converts to 1, "fred" converts to 2, "bob" converts to 3...

All of these are legitimate 'ways to convert a string to an integer value'. There are probably scores of other ways.
Badal Chowdhary
Ranch Hand

Joined: Apr 26, 2011
Posts: 34

If you really want to convert a String into int, you could also use hashcode method of String. But spend a good amount of time testing since you wanna encrypt a user's message which would be confidential.

Thanks


Blog: http://badalchowdhary.wordpress.com/
Twitter: http://twitter.com/badalrocks
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

one more point once you have encrypted the string you might want to decrypt it later..

so take the approach by which you can get it decrypted because as example shown below

1) Convert any 'a' to 1, 'b', to 2, 'c' to 3...and add them up, getting one single number


I don't think you will be able to decrypt it back. Security is also a major concern as Badal Metioned.


Do not wait to strike till the iron is hot; but make it hot by striking....
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Badal Chowdhary wrote:If you really want to convert a String into int, you could also use hashcode method of String


No, that won't work, because hashCode() is not a reversible function, and many Strings have the same hashCode.
Badal Chowdhary
Ranch Hand

Joined: Apr 26, 2011
Posts: 34

Jeff, Correct. I was imagining a hashcode function such that if objects are unequal, their hashcodes will never be same. But the hashcode contract doesn't conform to that.

Here's my imagination:

String s ="abd";
int hashCode = (1*1)+(2*2)+(3*4); // basically index_of_char * occurance_in_a-z with a==1 and z=26

But yes using hashcode method of String will lead to inconsistent results in the given scenario.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Badal Chowdhary wrote:Jeff, Correct. I was imagining a hashcode function such that if objects are unequal, their hashcodes will never be same. But the hashcode contract doesn't conform to that.


Not only that, but it's impossible to do that. There are exactly 2^32 possible hashCode() values. It should be obvious that there are more than that many possible Strings, and if it's not, just imagine this: String "1" could have hashCode value 1, String "999" could have hashCode value 999, etc. We can map all 2^32 hashCode values that way, and we still would never have used a single alphabetic character, so at that point, when we want a hashCode for "abc", it obviously has to reuse an existing one.

Here's my imagination:

String s ="abd";
int hashCode = (1*1)+(2*2)+(3*4); // basically index_of_char * occurance_in_a-z with a==1 and z=26


The problem with that is that char 2 in position 4 gives the same value as char 4 in position 2. That kind of thing can't be avoided completley, since we still have a much smaller number of possible hashCodes than number of possible input values, but there are ways to reduce those kinds of collisions, such as using a prime number for your multiplier.


Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

Badal Chowdhary wrote:Jeff, Correct. I was imagining a hashcode function such that if objects are unequal, their hashcodes will never be same. But the hashcode contract doesn't conform to that.

Here's my imagination:

String s ="abd";
int hashCode = (1*1)+(2*2)+(3*4); // basically index_of_char * occurance_in_a-z with a==1 and z=26

But yes using hashcode method of String will lead to inconsistent results in the given scenario.


As per specification mentioned in the class hash code doesn't calculated like this.

Its like s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]


where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7677
    
  19

Manoj Kumar Jain wrote:As per specification mentioned in the class hash code doesn't calculated like this.
Its like s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
where s[ i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation

And there's been a few improvements to String hashes since that one came about (specifically, ones that work a bit faster and still provide good bit spread).

The fact is, we still don't really know what adi wants from his "conversion", so the only thing you can say is that a unique int value (as might be returned by hashCode()) is impossible.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
The fact is, we still don't really know what adi wants from his "conversion"


Well, he did say:
i have to use this conversion in RSA algorithm, in which the user enters a message which is to be encrypted


but then he just repeated his original question and then vanished.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7677
    
  19

Jeff Verdegan wrote:Well, he did say...

Oh, OK.

Winston
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Jeff Verdegan wrote:There are exactly 2^32 possible hashCode() values. It should be obvious that there are more than that many possible Strings...


In fact there are not only "more than that many", there are "insanely astronomically more than that many". A String can be considered as a number in base 65,536 -- consider each of the chars in the string as a number between 0 and 65,535 and you get that number. And a String can have up to 2^32-1 chars in it, so you're looking at all of the numbers in base 65,536 which have up to 2^32-1 digits. There are (2^16) ^ (2^32-1) of those, which is 2 ^ (2^36-4).

That's a large number. In fact (if I have my calculations right) it's a number which has about 20 billion digits. (In base 10 of course.) And even if I screwed up my calculations by a factor of a few squigintillions it's still an insanely astronomically huge number.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7677
    
  19

Paul Clapham wrote:That's a large number. In fact (if I have my calculations right) it's a number which has about 20 billion digits

And hence the problem with equals() and hashCode().
To me, a hashcode has two possible uses:
1. A 32-bit check digit.
2. A bucket locator for a hashed collection.
And the problem is that the requirements for both have a lot of overlap.

To me, the best hashes are those that concentrate on property #1: distinctness.
Hashed collections can (and should) do all sorts of other stuff do deal with property #2 provided the first one is a given.

Winston
Badal Chowdhary
Ranch Hand

Joined: Apr 26, 2011
Posts: 34

Man! This is beating it to death stuff. But it's a good deep look. Thanks Jeff.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String to int