• 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

Convert String to primitive int without using Wrapper classes

 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
does anyone know how to convert String to primitive int without use of Wrapper classes.
Anil
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anil Verghese:

does anyone know how to convert String to primitive int without use of Wrapper classes.



Not sure I understand what you are asking. Do you want to do something like


without using the nice builtin parse functions?

The obvious way is to convert the String into a byte array, then iterate over the array. Pick up a byte, subtract "0" (ascii character zero) to get the value as an integer, then add it, and multiply by ten every time you pick up the next character.

Not sure this is really "advanced" so maybe I'm not understanding.
 
Anil Verghese
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I shall make my question clear to you..
For example
String s="12";
I want to know how this string can be converted to primitive int without using the methods of Wrapper Class ie Integer class methods should not be used to convert it to primitive.
Thanks in advance..
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like an interview question.
No serious code will try to do anything like that i believe.

Anyways check out the code in Integer.parseInt(), thats the best way to go

For your convenienece, here it is:

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Following program shows a logic to convert a string to int without using wrapper class. But this logic will work when input is only a positive number. For negative number input you can modify the logic.



[ August 21, 2007: Message edited by: prakash chandra ]
[ August 24, 2007: Message edited by: prakash chandra ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anil, I think it would help us to help you if you told us *why* you are interested in this.
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by prakash chandra:
For negative number input you can modify the logic.






Yes, that is essentially the same as my suggestion to just subtract ascii zero from the byte. Mine is faster and smaller.

Neither approach works for alphabets that encode digits with other characters.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pat Farrell:

Mine is faster and smaller.



Not that I think it matters, but are you sure that it's faster?
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:

Not that I think it matters, but are you sure that it's faster?



No, I'm never sure without hard cold benchmarks. And even then, I'm not sure that the benchmarks are measuring what I think they are measuring.

But


is likely to be slower than


Its less flexible, but when you don't care about radix, it skips a routine call setup/return. Code doesn't get much faster than subtracting a constant.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see. Thanks for the clarification!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic