Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
Can I increase an integer string ( string integer?) by one without converting it back to an integer then back to a string?
 
No. If you want an integer then use an integer. If you need that to be a string at specific times convert it to the string when you need it to be a string.
 
While I agree with robbieduncan (EDIT: and chrono1081) that it's probably not worth it, and better to keep an integer an integer, incrementing an integer string by one doesn't seem that difficult if you have to keep it in a string.

If the last digit is "0" to "8", replace it with "1" to "9" if it's "9" replace with "0" and repeat both rules for the next digit to the left. The worst case scenario is where you have a number like "99999999" and you need to increase the length of the string.

If it's an ASCII string, you've basically got an unpacked BCD number where the upper nibble is fixed as 0x30. So you might also be able to leverage some BCD library or other.

B
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.