0

code:

const char sbyte[] = {65, 66};

NSLog(@"byte:%c", 66);
NSLog(@"byte:%c", 67);
NSLog(@"byte:%s", sbyte);

NSString *string1 = [[NSString alloc] initWithCString:sbyte encoding:NSUTF8StringEncoding];
NSLog(@"string1: %@", string1);

NSString *string2 = [NSString stringWithFormat:@"%s", sbyte];
NSLog(@"string2: %@", string2);

print:

 byte:B
 byte:C
 byte:ABb
 string1: ABb
 string2: ABb

The correct application is 'AB', but now it is 'ABb', one more character 'b'??

thanks!

0

1 Answer 1

3

All C strings have to be zero terminated:

const char sbyte[] = {65, 66, 0};

They don't contain any length information therefore the zero is a way to detect end of data.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Thank you very much! can I find this here to know more detail?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.