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
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# Can we do this?

Ranch Hand
Posts: 629
• Number of slices to send:
Optional 'thank-you' note:
Convert a String say, "Hello" to an integer primitive type?

Thanks.

Bartender
Posts: 10336
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

Thanks.

Ranch Hand
Posts: 1374
• Number of slices to send:
Optional 'thank-you' note:
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 ]

author
Posts: 14112
• Number of slices to send:
Optional 'thank-you' note:
With other words, what do you want the result to be?

Rancher
Posts: 3742
16
• Number of slices to send:
Optional 'thank-you' note:
String.hashCode() will also give you an integer value.

lowercase baba
Posts: 13050
67
• Number of slices to send:
Optional 'thank-you' note:

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.

Ranch Hand
Posts: 202
• Number of slices to send:
Optional 'thank-you' note:
you can't convert "hello" in 10 for example. the that you can is "20" in 20.
as a Arjun Reddy spoke.

Marshal
Posts: 75719
354
• Number of slices to send:
Optional 'thank-you' note:
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
Marshal
Posts: 75719
354
• Number of slices to send:
Optional 'thank-you' note:

Arjun Reddy
Ranch Hand
Posts: 629
• Number of slices to send:
Optional 'thank-you' note:

Ranch Hand
Posts: 117
• Number of slices to send:
Optional 'thank-you' note:

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

 You didn't tell me he was so big. Unlike this tiny ad: Free, earth friendly heat - from the CodeRanch trailboss https://www.kickstarter.com/projects/paulwheaton/free-heat