Zero Padding String

Discussion in 'Mac Programming' started by wrayal, Aug 18, 2009.

  1. wrayal macrumors newbie

    Joined:
    Aug 23, 2008
    #1
    Hi,

    I need to output some numbers to an NSTextField. The numbers are unsigned short ints, and I need to output them in the form ****h in hexadecimal, where number is zero-padded at the beginning if it is <0x1000. Currently, I'm using:

    [AFbox setStringValue:[NSString stringWithFormat:mad:"%4Xh", [cpu AF]]];

    But I really can't see a good way of zero-padding the string...can someone point me in the right direction? ([cpu AF returns the unsigned short ints).

    Many thanks,

    Wrayal

    [edit: I have tried stringByPaddingToLength:withString:startingAtIndex:, but this will only pad at the end as far as I can tell]
     
  2. harry65 macrumors member

    Joined:
    Aug 26, 2008
    #2
    Try

    Code:
    [AFbox setStringValue:[NSString stringWithFormat:@"%04Xh", [cpu AF]]];
    
    The '0' before the '4' specifies what you want your padding character to be.

    Edit:

    I actually made it sound like you can choose any padding character you want... I don't think that's the case. I do know that putting a '0' here will pad with 0's
     
  3. wrayal thread starter macrumors newbie

    Joined:
    Aug 23, 2008
  4. harry65 macrumors member

    Joined:
    Aug 26, 2008

Share This Page