简体   繁体   English

如何在NSMutableData的末尾附加3个字节的“ 110”

[英]How to append 3 bytes '110' at the end of NSMutableData

HERE IS THE CODE-> 代码在这里->

NSString* message = @"username,password";
NSMutableData *data = [[NSMutableData alloc] initWithData:
                       [message dataUsingEncoding:NSASCIIStringEncoding]];

[data appendBytes:"0x1""0x1""0x0"
               length:3];

I am expecting output -> username,password110 我期望输出-> username,password110

but not getting this. 但没有得到这个。

What you are appending is not bytes, it's a string "0x10x10x0" . 您要附加的不是字节,而是字符串"0x10x10x0" The first three characters get appended - specifically, '0' , 'x' , and '1' . 前三个字符被附加-分别是'0''x''1'

To append three bytes, create a byte array, and append it: 要附加三个字节,请创建一个字节数组,然后附加它:

unsigned char suffixBytes[] = {1, 1, 0};
[data appendBytes:suffixBytes length:3];

In one specific case, when all bytes represent character codes of printable characters, you can use a string literal instead: 在一种特定情况下,当所有字节均表示可打印字符的字符代码时,可以改用字符串文字:

[data appendBytes:"110" length:3];

This will append character codes of '1' , '1' , and '0' to your data. 这会将字符代码'1''1''0'附加到您的数据中。 Assuming ASCII encoding, the values would be {0x31, 0x31, 0x30} . 假设使用ASCII编码,则值为{0x31, 0x31, 0x30}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM