# String to int

adi bashir

Greenhorn

Posts: 9

posted 5 years ago

hi everyboby. can you please tell me how to convert string like "adil" to an integer type?

adi bashir

Greenhorn

Posts: 9

posted 5 years ago

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 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

Posts: 9

posted 5 years ago

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.

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 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.

posted 5 years ago

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

Thanks

posted 5 years ago

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

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

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....

posted 5 years ago

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.

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.

posted 5 years ago

Not only that, but it's

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.

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.

posted 5 years ago

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

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

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

posted 5 years ago

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

Winston

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

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

posted 5 years ago

Well, he did say:

but then he just repeated his original question and then vanished.

Winston Gutkowski wrote:

The fact is, westilldon'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.

posted 5 years ago

Oh, OK.

Winston

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

Oh, OK.

Winston

Articles by Winston can be found here

posted 5 years ago

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

- 1

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.

posted 5 years ago

And hence the problem with

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

To me, the best hashes are those that concentrate on property #1:

Hashed collections can (and should) do all sorts of other stuff do deal with property #2

Winston

Paul Clapham wrote:That's a large number. In fact (if I have my calculations right) it's a number which hasabout 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

Articles by Winston can be found here