# Thread: Using Atoi and missing 0's

1. ## Using Atoi and missing 0's

I just figured out how to finally get the Atoi to work in my program but I can't seem to figure out how to make sure that it doesn't get rid of the 0's. For example i want to get 000012345 into an int BUT it just displays 12345 and deletes the 0. Anyway around this?

2. Originally Posted by Shadowwoelf
I just figured out how to finally get the Atoi to work in my program but I can't seem to figure out how to make sure that it doesn't get rid of the 0's. For example i want to get 000012345 into an int BUT it just displays 12345 and deletes the 0. Anyway around this?
The premise of the entire question is wrong. There is no such thing as "leading zeroes" on a value. A value is what it is. When it is written in a particular base it takes on a particular representation. The leading zeros are part of the representation, not the value.

atoi() converts the representation to a value, which clearly is going to discard the irrelevant leading zeros. There is no way to get them back, and I don't see why you'd want to.

If you want to print it back out exactly as it was entered, don't convert it to an integer in the first place.

3. if you just want to display the number, your best option would be to print the original string to the screen rather than converting it to an int

eh... 3 minutes... I'm slow...[/edit]

4. It is possible to format numbers to be a specific width, right align them, and fill the space with zeros. But as others have said, why not just print the original string?

5. I had the same problem when trying to read a string from a file and attempt to match it to an integer, I decided to append a 1 to the start of the value, assuming that the value would not exceed the max size of an int. Not sure if that helps what you're trying, though.

6. Every number has an infinit number of leading 0's.
All these numbers are exactly the same:
3.14
0003.14
3.14000000
00000000000000000000000000003.14000000000000000000 00000000

7. > It is possible to format numbers to be a specific width, right align them, and fill the space with zeros.
Like
printf( "&#37;06d", mynumber );

In C++, use the setw() and setfill() manipulators.
http://www.cplusplus.com/reference/i...tors/setw.html
http://www.cplusplus.com/reference/i...s/setfill.html

8. Well Sorry guys I had no Idea what I was thinking must have lost my brain when I wrote that heh. By the way BlackRoot thats the exact same problem I am dealing with right now but I can just assume that there wont be any 0's in the front of the password

9. Password? Why on earth would you convert a password to an integer?

Popular pages Recent additions