I am getting some weird numbers returned from the characterAtIndex method for Object-C in the iPhone SDK. The code snippet is as follows:

 NSString *new_text;
 new_text = [new_text stringByAppendingFormat:@"%@",UITextView_1.text];//=a,b,...f

 for(int i = 0; i <= 6; i++)
   char_num   = [new_text characterAtIndex:i];   

I am trying to get the decimal number for the individual character. I am either getting the wrong value each time. Sometimes the value is greater than 8,000! I should be getting back the number 65 through 71.

+1  A: 

new_text is nil when you send the stringByAppendingFormat to it. It's legal to send a message to a nil object-- it simply does nothing. Thus new_text is never initialized, hence the weird values.

if new_text was an initialized NSMutableString your code would be ok:

// stringByAppendingFormat is an instance method and must be called on a non-nil instance
NSMutableString* text = [NSMutableString string];
text = [text stringByAppendingFormat:@"%@",UITextView_1.text];

// stringWithFormat is a class method and can be used to initialize an instance
NSString* text = [NSString stringWithFormat:@"%@",UITextView_1.text];
But I am trying to get the decimal value for each character (A=65, B=66, etc.) . How will these string methods give me back those values?
Kevin McFadden
The string methods merely make the string. The characterAtIndex is what gets you the values. try NSLog(@"%d", [@"A" characterAtIndex:0]);
The NSLog seems to work on the first two characters in the string. The third one character returned a value of 8656. Could it be returning an address instead of a decimal value?
Kevin McFadden
I've tried to add a bit more detail to what I am doing. NSInteger char_num; NSString *new_text; new_text = [new_text stringByAppendingFormat:@"%@",UITextView_1.text]; for(int i = 0; i <= working_text_length-1; i++) { char_num = [UITextView_1.text characterAtIndex:i]; new_text = [encrypted_text stringByAppendingFormat:@"%c",new_key_char_num]; }UITextView_1.text = KEVINK=75E=69V=86I=73N=78THe Decimal values that I get are 34, 8565, 117, 707, 93
Kevin McFadden
You're still sending stringByAppendingFormat to a nil string. Change this to: NSString* new_text = [NSString stringWithString:UITextView_1.text]; Even better, just iterate over UITextView_1.text.
WHen I iterate over the UITextView text, I get the same results. This is just crazy! My code is really straight forward.
Kevin McFadden
is this possibly Unicode text? in that case some characters would be larger than 8 bits
I believe your UITextView_1.text is in someway not an ASCII string. Look at it in the debugger and check this.
+1  A: 

new_text is a local variable, it's not initalized, and you send a message to it before assigning a value to it. Unlike newly alloc'd Objective-C objects, local variable values are not zero'd automatically, thus the pointer value of new_text is whatever happened to be that memory location prior to the method's execution. In other words, your new_text pointer is pointing at a non-deterministic memory location, thus the behavior here is undefined and unpredictable. Usually you'd end up crashing, but by sheer chance you're pointing at something that looks like a string, but the contents of that string are also non-deterministic. Hence weird character values.

n.b. This is a great example of why it's good practice to always initialize local variables when they're declared.

Neil Mix