Send 0xF8 byte over TCP

521 views Asked by At

I am using Asyncsocket to send bytes over TCP. I have the code working, however when it comes to send byte 0xF8, I am having issue.

Here is what I have try(in the comment below) and the codes:

NSString *ip_current = self.text_ip.text;
sockethost=ip_current;
socketport=9100;
[self   connect];

//a working one as long as I don't use 0xF8
NSData   *dataStream  = [[NSString stringWithFormat:@"%c%c",0x1d,0x31] dataUsingEncoding:NSUTF8StringEncoding];
[self.socket writeData:dataStream withTimeout:1 tag:1];

 //This one below will give 0x1d, 0xc3,0xb8 (After some research I found that this is due to encoding issue with UTF8)
 NSData   *dataStream  = [[NSString stringWithFormat:@"%c%c", 0x1d, 0xf8 dataUsingEncoding:NSUTF8StringEncoding];
//People suggest that using ASCII encoding, this time I don't get 3 bytes but 2 bytes with wrong data with 1d and 6f(f8 change to 6f)
NSData   *dataStream  = [[NSString stringWithFormat:@"%c%c%c",0x1d,0xf8,0x31] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

[self.socket disconnectAfterWriting];

So, whatever encoding I am using, there is something wrong. Does anybody know how to fix this? Thanks in Advance!!!

1

There are 1 answers

0
rmaddy On BEST ANSWER

You just want a byte. Strings have nothing to do with this. Create your NSData from the byte, not a string.

uint8_t byte = 0xF8;
NSData *dataStream = [NSData dataWithBytes:&byte length:1];