How to use NULL character with NSString?

前端 未结 5 1035
梦毁少年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: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];
    

提交回复
热议问题