How to use a NULL character using NSString?

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

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

How to create data from a string with NULL characters?

This does not work...

 NSData * authCode = [[NSString stringWithFormat:@"%c%@%c%@", '\0', self.username, '\0', self.password] dataUsingEncoding:NSUTF8StringEncoding]; 
+4
source share
5 answers

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 string]; [string initWithBytes:bytes length:sizeof(bytes)]; NSData * data = [string dataUsingEncoding:NSUTF8StringEncoding]; 

You can see null bytes at the beginning, between username / password and at the end - because char[] has a null termination.

+4
source

Your syntax is correct. NSString just does not handle NULL bytes. I cannot find documentation about this, but NSString silently ignores %c format specifiers with argument 0 (and in this note the character constant '\0' expands to integer 0 , which is true). However, it can handle \0 directly embedded in the 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: since stringByEscapes actually contains NULL bytes, it ends the NSLog string early).

+1
source

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
source

StefanB's answer looks like the right option. It turns out that I did not get the wrong information, so that it looked like this: \ 0 did not work

This was normal:

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

You can 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
source

Source: https://habr.com/ru/post/1299028/


All Articles