How to use NULL character with NSString?

前端 未结 5 1037
梦毁少年i
梦毁少年i 2021-01-19 05:12

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

Now, in Objecti

相关标签:
5条回答
  • 2021-01-19 05:21

    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"

    0 讨论(0)
  • 2021-01-19 05:30

    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).

    0 讨论(0)
  • 2021-01-19 05:31

    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 讨论(0)
  • 2021-01-19 05:34

    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.

    0 讨论(0)
  • 2021-01-19 05:36

    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];
    
    0 讨论(0)
提交回复
热议问题