backbencher
backbencher

Reputation: 109

How to append leading zeroes to an NSString?

I have seen multiple answers but those have converted an integer into a string and so on.

I have an NSString and want to add leading zeroes to it.

NSString *hexString = [NSString stringWithFormat:@"%2X",abc];

add zeroes to hexString if its length is less than 3 and return another string. Appreciate help.

Upvotes: 3

Views: 638

Answers (1)

rmaddy
rmaddy

Reputation: 318814

If you want to convert the int value abc into a 3-digit hex value with leading zeros to ensure it uses 3 digits, then do:

NSString *hexString = [NSString stringWithFormat:@"%03X", abc];

Upvotes: 6

Related Questions