开发者

How to use NULL character with NSString?

In PHP, I can call base64_encode("\x00". $username. "\x00". $password) and the "\x00" represents a NULL character.

Now, in Objective-C, I have a function that converts NSData to base64 encoded NSString created by DaveDribin.

How do I create data from a string that has NULL characters?

This doesn't seem to work...

NSData * authCode = [[NSString stringWithFormat:@"%c%@%c%@", '\0', self.username, '\0', self.password] dataUsingEncoding:NS开发者_开发技巧UTF8StringEncoding];


Like this:

char bytes[] = "\0username\0password";
NSData * data = [NSData dataWithBytes:bytes length:sizeof(bytes)];

NSLog(@"%@", data);

Output:

2010-01-22 09:15:22.546 app[6443] <00757365 726e616d 65007061 7373776f 726400>

Or from NSString:

char bytes[] = "\0username\0password";
NSString * string = [[NSString alloc] initWithBytes:bytes length:sizeof(bytes) encoding:NSUTF8StringEncoding];
NSData * data = [string dataUsingEncoding:NSUTF8StringEncoding];

You can see the null bytes at the beginning, in between username/password and at the end - because the char[] is null terminated.


Your syntax is correct. NSString just doesn't handle NULL bytes well. I can't find any documentation about it, but NSString will silently ignore %c format specifiers with an argument of 0 (and on that note, the character constant '\0' expands to the integer 0; that is correct). It can, however, handle \0 directly embedded into an NSString literal.

See this code:

#import <Cocoa/Cocoa.h>
int main (int argc, char const *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSString *stringByChars = [NSString stringWithFormat:@"-%c%c%c%c-",0,0,0,0];
    NSString *stringByEscapes = [NSString stringWithFormat:@"-\0\0\0\0-"];
    NSLog(@"  stringByChars: \"%@\"", stringByChars);
    NSLog(@"            len: %d", [stringByChars length]);
    NSLog(@"           data: %@", [stringByChars dataUsingEncoding:NSUTF8StringEncoding]);
    NSLog(@"stringByEscapes: \"%@\"", stringByEscapes);
    NSLog(@"            len: %d", [stringByEscapes length]);
    NSLog(@"           data: %@", [stringByEscapes dataUsingEncoding:NSUTF8StringEncoding]);
    [pool drain];
    return 0;
}

returns:

  stringByChars: "--"
            len: 2
           data: <2d2d>
stringByEscapes: "-
            len: 6
           data: <2d000000 002d>

(Note that since the stringByEscapes actually contains the NULL bytes, it terminates the NSLog string early).


Not quite sure why this works, but instead of using @"%c" as the format string with '\0', try using @"%@" as the format string with @"\0"


You could also try this (tested and working - taken from: http://www.cocoabuilder.com/archive/cocoa/174917-nul-characters-in-nsstring-cause-unexpected-results.html)

NSString* s1 = [[NSString alloc] initWithBytes:buffer length:sizeof (buffer)
      encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF16LE)]; // @"A[NUL]end" (*)
NSLog(@"s1 = %@", s1);
NSString* s2 = @"CD";
NSLog(@"s2 = %@", s2) ;
NSString* sC = [s1 stringByAppendingString:s2];
NSLog(@"sC = %@", sC);
NSLog(@"length of s1:%i", [s1 length]);
NSLog(@"length of s2:%i", [s2 length]);
NSLog(@"length of sC:%i", [sC length]);
[s1 release];


stefanB's answer looks like a right option. Turns out I was passing in wrong info to make it look like \0 wasn't working

This was ok:

[NSString stringWithFormat:@"\0user\0pass"]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜