0

I'm trying to port the following Java code to Objective C.

myByteBuffer = new ByteArrayOutputStream(myCfA.length() + 1);
myByteBuffer.write(myCfA.getBytes());
myByteBuffer.write(new byte[]{0});
myWriter.write(myByteBuffer.toByteArray());
myWriter.flush();

It's relevant Objective-C code

NSInteger strLength = self.myCfA.length;
NSMutableString *response  = [NSMutableString stringWithCapacity:strLength];
[response appendString:self.myCfA];
[response appendString:@"0"];
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSUTF8StringEncoding]];
NSInteger bytesCount = [self.outputStream write:[data bytes] maxLength:[data length]]; 

Is my approach of taking NSMutableString and then converting into NSData and getting bytes a correct approach? Also for new byte[]{0} I'm directly appending 0 as string. Please correct me if I'm going wrong. There is an issue here which I'm unable to trace out.

Thanks Sudheer

Lucas Eduardo
  • 11,525
  • 5
  • 44
  • 49
Dantuluri
  • 685
  • 1
  • 10
  • 23

1 Answers1

0

self.myCfA is a string and you can directly convert that to NSData. So the code given below will give you the same result.

NSData *data = [self.myCfA dataUsingEncoding:NSUTF8StringEncoding]
NSInteger bytesCount = [self.outputStream write:[data bytes] maxLength:[data length]];

Update: To append byte to the existing data.

NSMutableData *data = [NSMutableData dataWithData:[self.myCfA dataUsingEncoding:NSUTF8StringEncoding]];
[data appendBytes:[@"\0" UTF8String] length:1];
NSInteger bytesCount = [self.outputStream write:[data bytes] maxLength:[data length]];
Augustine P A
  • 5,008
  • 3
  • 35
  • 39