Daniel Klöck
Daniel Klöck

Reputation: 21137

Send a NSString with \0

I am trying to send a string terminated with \0 to a tcp socket but it seems the \0 does never reach its destination.

Using this code:

NSString* s=@"<b/> \0_";

uint8_t *buf = (uint8_t *)[s UTF8String];

int resCode = [outputStream write:buf maxLength:strlen((char *)buf)];

I only seem to send @"<b/> ". Probably the \0 is seen as the end of the uint8_t.

Can someone tell me how it should be done??

Upvotes: 0

Views: 1062

Answers (2)

sha
sha

Reputation: 17860

Of course it sends it without \0. strlen will return you length of the string, not counting null-terminators. Change it to something like

[outputStream write:buf maxLength:(strlen((char *)buf) + 1)]

Upvotes: 4

SPatil
SPatil

Reputation: 1003

Why do you want to send ']0] character in your String.....

Can't you use any other character...because '\0' character is always treated as terminating character!

And if you really want to send it that way then send a different character and map it to '\0' when you receiv it!

Upvotes: -2

Related Questions