2

This has got to be simple -- surely this method is supposed to work -- but I'm having some kind to two-byte-to-one-byte problem, I think.

The purpose of the code is to generate a string of 0 characters of a certain length (10 minus the number of digits that will be tacked onto the end). It looks like this:

const unichar zero = 0x0030;
NSString *zeroBuffer = [NSString stringWithCharacters:&zero length:(10 - [[NSString stringWithFormat:@"%i", photoID] length])];  

Alternate second line (casting the thing at address &zero):

NSString *zeroBuffer = [NSString stringWithCharacters:(unichar *)&zero length:(10 - [[NSString stringWithFormat:@"%i", photoID] length])];

0x0030 is the address of the numeral 0 in the Basic Latin portion of the unicode table.

If photoID is 123 I'd want zeroBuffer to be @"0000000". What it actually ends up as is a zero and then some crazy unicode characters along the lines of (not sure how this will show) this:

0䪨 燱ܾ뿿﹔

I'm assuming that I've got data crossing character boundaries or something. I've temporarily rewritten it as a dumb substring thing, but this seems like it would be more efficient.

What am I doing wrong?

Matthew Frederick
  • 22,245
  • 10
  • 71
  • 97

1 Answers1

4

stringWithCharacters:length: expects the first argument to be the address of a buffer containing each of the characters to be inserted in the string in sequence. It's reading your character zero for the first character, then advancing to the following memory address and reading whatever data is there for the next character, and so on. This is not the right method for doing what you're trying to do.

Alas, there isn't a built-in repeat-this-string method. See the answers here for suggestions.

Alternatively, you can avoid the issue completely and just do this:

[NSString stringWithFormat:@"%010i", photoID];

That causes the number formatter to output a decimal number padded with ten zeroes.

Community
  • 1
  • 1
grahamparks
  • 16,130
  • 5
  • 49
  • 43
  • Ah, that explains everything, thanks, no more hair-pulling. I probably should have realized that "length" doesn't means the same thing as "count". :) Your alternate solution is perfect for my needs. FWIW I'd settled on [@"0000000000" substringToIndex:(10 -... which worked but wasn't pretty. However, it might be the fastest and least memory-intensive (and no mutable strings!) if I needed a character besides 0, it was just one repeating character, and I knew it had a reasonably small maximum length. Thanks again. – Matthew Frederick Nov 16 '10 at 03:37