• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String to int

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyboby. can you please tell me how to convert string like "adil" to an integer type?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
adi bashir
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 34
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 34
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Oh, OK.

Winston
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 34
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Man! This is beating it to death stuff. But it's a good deep look. Thanks Jeff.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic