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
• Tim Cooke
• Junilu Lacar
Sheriffs:
• Rob Spoor
• Devaka Cooray
• Jeanne Boyarsky
Saloon Keepers:
• Jesse Silverman
• Stephan van Hulst
• Tim Moores
• Carey Brown
• Tim Holloway
Bartenders:
• Jj Roberts
• Al Hobbs
• Piet Souris

# Convert string to integer without any API ?

Ranch Hand
Posts: 798
• Number of slices to send:
Optional 'thank-you' note:
if I have a string as "12345", then how to convert it to integer ? we are NOT allowed to use any Integer convert API ?

Thanks.

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

Edward Chen wrote:if I have a string as "12345", then how to convert it to integer ? we are NOT allowed to use any Integer convert API ?

Check out this formula,

int n = 1*10000 + 2*1000 + 3*100 + 4*10 + 5;

and figure out how you go from "12345" to there.

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

uj nossnahoj wrote:
Check out this formula,

int n = 1*10000 + 2*1000 + 3*100 + 4*10 + 5;

and figure out how you go from "12345" to there.

No, I don't think this is correct solution. At least, we need to cast char to int , or do case-switch. Maybe has a better solution.

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

Edward Chen wrote:No, I don't think this is correct solution. At least, we need to cast char to int , or do case-switch. Maybe has a better solution.

It's part of a correct solution. What's left are minor details like the conversion of a digit from an ASCII representation to an integer representation for example.

It can be done like this,

char c; // a digit between '0' and '9';
int i = ((int)c) - ((int)'0'); // a digit between 0 and 9.

Sheriff
Posts: 22504
122
• Number of slices to send:
Optional 'thank-you' note:

uj nossnahoj wrote:char c; // a digit between '0' and '9';
int i = ((int)c) - ((int)'0'); // a digit between 0 and 9.

No need for the cast; char can be implicitly converted to int. Besides, the result of char + char is int anyway.

Marshal
Posts: 74341
334
• Number of slices to send:
Optional 'thank-you' note:
Agree with Rob. TryIf you miss out the + 1 you get 25 and 0.

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

Rob Prime wrote:No need for the cast; char can be implicitly converted to int. Besides, the result of char + char is int anyway.

Just because you can omit something doesn't mean you should. I'm very much in favour of explicit casting because it clearly demonstrates your intentions and it removes many possible conversion mistakes like unintentional integer division to name just one.

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

Edward Chen wrote:No, I don't think this is correct solution. At least, we need to cast char to int , or do case-switch. Maybe has a better solution.

Here's another hint. Note that the formula I posted can be rewritten like,

int n = (((1*10 + 2)*10 + 3)*10 + 4)*10 + 5;

Can you see the pattern? If you consider the digits from left to right you're building up the number in steps. In each step you're multiplying the number from the previous step by ten and adding a digit, like

Campbell Ritchie
Marshal
Posts: 74341
334
• Number of slices to send:
Optional 'thank-you' note:
I would agree with Rob; the cast is unnecessary and makes the code confusing.

Ranch Hand
Posts: 492
• Number of slices to send:
Optional 'thank-you' note:
I would also agree that there is no need to cast since the characters already have integer values in the ascii table.

I'm going to try and clarify what Uj is doing here a little to see if it helps.

First step:
Look at the ascii table and determine the values for the characters 1,2,3,4,5

Second step:
Still looking at the ascii table find a value that if you subtract the char values of 1,2,3,4,5 by it you will get the integers 1,2,3,4,5;
Ex: char value of 1 - 'some value' = 1

Step Three:
After you have found those values look closely at the way Uj expanded the number 12345 in his earlier post and see if you can build an integer from that.
i.e.

uj nossnahoj wrote:

Check out this formula,

int n = 1*10000 + 2*1000 + 3*100 + 4*10 + 5;

and figure out how you go from "12345" to there.

Hope this helps,
Hunter

Campbell Ritchie
Marshal
Posts: 74341
334
• Number of slices to send:
Optional 'thank-you' note:
Thank you everybody. I am sure that must be sufficient information to calculate an int value.