# Can we do this?

Arjun Reddy
Ranch Hand
Posts: 629
Convert a String say, "Hello" to an integer primitive type?

Thanks.

Paul Sturrock
Bartender
Posts: 10336
Is this a homework question? What rules do you want to follow to create this value? (i.e. h == 8, e == 5 etc. or something else?)
[ June 16, 2008: Message edited by: Paul Sturrock ]

Arjun Reddy
Ranch Hand
Posts: 629
Um.. it's not a homework question. I was doing this,

String s = "20";
int i = Integer.parseInt(s);

And then I tried this,

String s = "hello";
int i = Integer.parseInt(s);
and I got a NumberFormatException. So I was wondering if there is a way I can convert it to int.

Thanks.

Paul Sturrock
Bartender
Posts: 10336
Yes there are a number of ways you can convert it to an int. First thing you need to do is define the rules for this conversion, since "Hello" doesn't have any implicit numeric value. What rules are you going to use?

Arjun Reddy
Ranch Hand
Posts: 629

Thanks.

Vikas Kapoor
Ranch Hand
Posts: 1374
I think by 'Rule' Paul means your own rules.

You can convert the string into integer like,

1) String = hello ---> Integer = 8,5,12,12,15 (A to Z == 1-26)
2) String = hello ---> Integer = 104,101,108,108,111 (Ascii value)
3) String = HELLO ---> Integer = 72,69,76,76,79 (Ascii value)
[ June 16, 2008: Message edited by: Vishal Pandya ]

Ilja Preuss
author
Sheriff
Posts: 14112
With other words, what do you want the result to be?

Joanne Neal
Rancher
Posts: 3742
16
String.hashCode() will also give you an integer value.

fred rosenberger
lowercase baba
Bartender
Posts: 12228
36
Originally posted by Arjun Reddy:
Convert a String say, "Hello" to an integer primitive type?

Java is a very flexible language. you can do all kinds of stuff with it. The difficulty here is that your question doesn't really make a lot of sense. It's kind of like saying "can i convert the string "hello" to a color? or a sound? or a taste?"

YOU have to decide what you mean by converting it to an int. you need to define what is called an 'algorithm' - think of it like a recipe. you would have to define the step by step instructions of what exactly the computer (or a person) should do.

You could convert each letter into some numeric equivalent then sum the total. you could multiply them all together. you could use something more complicated if you wanted a better chance of getting a distinct number for each string.

Then you need to think about case.... is "Hello" treated the same as "HELLO" treated the same as "hello"? What about punctuation?

The difficulty with your post is that the problem is just not well defined. think about these things, and make some decisions. If you then get stuck, come back and ask more questions.

camilo lopes
Ranch Hand
Posts: 202
you can't convert "hello" in 10 for example. the that you can is "20" in 20.
as a Arjun Reddy spoke.

Campbell Ritchie
Sheriff
Posts: 50644
83
You can try using base 36 arithmetic, which allows you to parse any letter, a=10, z=35. It might work with H as well; I have never tried. But you can't use base 37 arithmetic!
Integer.parseInt("hello", 36);

Might work, might not.

Campbell Ritchie
Sheriff
Posts: 50644
83

Arjun Reddy
Ranch Hand
Posts: 629

Ronald Schild
Ranch Hand
Posts: 117
Originally posted by Campbell Ritchie:
You can try using base 36 arithmetic, which allows you to parse any letter, a=10, z=35. It might work with H as well; I have never tried. But you can't use base 37 arithmetic!
Integer.parseInt("hello", 36);

Might work, might not.

It works!

Even works!

Thanks Ritchie, fun