• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert string to integer without any API ?

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Rob. TryIf you miss out the + 1 you get 25 and 0.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would agree with Rob; the cast is unnecessary and makes the code confusing.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everybody. I am sure that must be sufficient information to calculate an int value.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic