increasing string integers

Discussion in 'Mac Programming' started by farmerdoug, Mar 9, 2011.

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    Can I increase an integer string ( string integer?) by one without converting it back to an integer then back to a string?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
  4. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #4
    +1 This is the best way to do it.
     
  5. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #5
    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
     

Share This Page