0

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!!!

Rabbit
  • 3
  • 2
  • Don't use strings for this. Your data is not a string. – rmaddy Nov 11 '14 at 23:35
  • OK, but which method should I use? I am also new to object C, could you show me please? This is also a question I would like to ask, except NSData *dataStream = [[NSString stringWithFormat:@"%c%c", 0x1d, 0xf8 dataUsingEncoding:NSUTF8StringEncoding]; using NSString here, what should I use to pass F8? – Rabbit Nov 11 '14 at 23:36

1 Answers1

1

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];
rmaddy
  • 314,917
  • 42
  • 532
  • 579