There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
adi bashir wrote:for example i want to convert string "adil" to its own calculated integer value
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?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
1) Convert any 'a' to 1, 'b', to 2, 'c' to 3...and add them up, getting one single number
Do not wait to strike till the iron is hot; but make it hot by striking....
Badal Chowdhary wrote:If you really want to convert a String into int, you could also use hashcode method of String
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
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.
Do not wait to strike till the iron is hot; but make it hot by striking....
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
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
The fact is, we still don't really know what adi wants from his "conversion"
i have to use this conversion in RSA algorithm, in which the user enters a message which is to be encrypted
Jeff Verdegan wrote:There are exactly 2^32 possible hashCode() values. It should be obvious that there are more than that many possible Strings...
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
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
This tiny ad is guaranteed to be gluten free.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|