This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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?
Joined: Feb 06, 2012
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?
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
Joined: Feb 06, 2012
for example i want to convert string "adil" to its own calculated integer value or i can say ASCII value like 56?
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.
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.
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.
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 wrote:As per specification mentioned in the class hash code doesn't calculated like this.
Its like s*31^(n-1) + s*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 uniqueint value (as might be returned by hashCode()) is impossible.
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 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.
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.