[英]How to append 3 bytes '110' at the end of NSMutableData
代碼在這里->
NSString* message = @"username,password";
NSMutableData *data = [[NSMutableData alloc] initWithData:
[message dataUsingEncoding:NSASCIIStringEncoding]];
[data appendBytes:"0x1""0x1""0x0"
length:3];
我期望輸出-> username,password110
但沒有得到這個。
您要附加的不是字節,而是字符串"0x10x10x0"
。 前三個字符被附加-分別是'0'
, 'x'
和'1'
。
要附加三個字節,請創建一個字節數組,然后附加它:
unsigned char suffixBytes[] = {1, 1, 0};
[data appendBytes:suffixBytes length:3];
在一種特定情況下,當所有字節均表示可打印字符的字符代碼時,可以改用字符串文字:
[data appendBytes:"110" length:3];
這會將字符代碼'1'
, '1'
和'0'
附加到您的數據中。 假設使用ASCII編碼,則值為{0x31, 0x31, 0x30}
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.