1

I am getting null character in the middle of the string obtained as a part of response to HTTP Post. As a result the content gets printed only upto the null character even though the string has more data in it. Below is the sample code that illustrates the problem

 NSString *testString= @"v1db1���������¿¿sssss"; ->this line shows the null character warning
 NSInteger stringlength = [testString length];
 NSLog(@"String Length:%ld",stringlength);
 NSLog(@"String Value:%@",testString);

Note: the test string contains a null character with the question mark. For some reason I am not able to save the post if I copy the exact string.

The first line shows a warning "Null character(s) preserved in string literal" in Xcode.

The output of this program is

 String Length:21
 String Value:v1db1

What is the correct approach to solve this problem?. I am thinking of scanning the NSString for any null character and removing the same. What could be the reason I am getting null character ?.

Sriram
  • 21
  • 4
  • How do you, in code, obtain the NSString from the HTTP Post response? It is likely that is the source of your problem, you should not be creating an NSString with a null in it in the first place. – CRD Feb 18 '12 at 18:48
  • @CRD Thanks.This data is returned by the server and null is not inserted.I am using content-type from the HTTP header to the determine the encoding of the response data. I am using **bold**(CFReadStreamCreateForHTTPRequest) for the http request if that helps. Somehow the data read contains null character in between. Even if it is the problem in the server , the client is expected to handle it. I found a thread where a similar problem being dicussed [link](http://www.cocoabuilder.com/archive/cocoa/174917-nul-characters-in-nsstring-cause-unexpected-results.html?q=NUL+strings#174917). – Sriram Feb 19 '12 at 07:09
  • Add the code you actually use to your question. It is quite possible that they way you are converting the bytes you read to an NSString is the cause of your problem. Until folks see your actual code they've little chance of helping you. You might also look at printing out the bytes you actually read in hex to see what is arriving from the server. – CRD Feb 21 '12 at 09:50

1 Answers1

0

The problem characters are UTF-8 encoded something (EF BF BD and C2 BF) that are handled badly directly in quoted strings in XCode. You will need to convert it. Something like:

[ NSString stringWithUTF8String: [ @"v1db1���������¿¿sssss" cStringUsingEncoding: [ NSString defaultCStringEncoding ] ] ];
colbadhombre
  • 803
  • 8
  • 11
  • Turns out that the encoded chars are just the way unknown chars are encoded in UTF-8, you'll have to convert the chars immediately to have a chance of seeing them. By the time they got to the string literal, they had already been lost. – colbadhombre Feb 18 '12 at 16:14
  • Thanks for the quick reply. I have already tried this but does not work. I am not able to copy the exact string for some reason . Imagine if the testString is like testString = "Some text +null character + some text 2". The output I am getting is some text. This is the first time I have encountered something like this – Sriram Feb 18 '12 at 17:37
  • Null characters are how C strings are terminated. If you are saying that your HTTP response contains one, then you'd have to look at the server side to find out why it is happening. If you are looking to keep your HTTP client side from thinking that the string terminates at the null char, then you have to break it yourself, or substitute the null with something else. – colbadhombre Feb 18 '12 at 18:30
  • Thanks. Looking from the server is not an option. The code is a part of a complex authentication logic and the server is highly dynamic. I am finding this problem only with one specific server in one particular scenario. Replacing null characters would be last resort unless I find something better. – Sriram Feb 19 '12 at 07:15