Can anyone help me with appending the nsdata
i have NSData as encrypted data
<a5ecaf36 15519fcb cd164cf6 a83eaf55 367f1109 b6898951 2c227b86 ed98d0a6 db2d7a8a 25086baf 58436328 2583ed78 ef3410e5 4507d8a4 40fe22f9 0538a67c 065fbb8e ad445041 56f3ea87 e7b73189 aa0b8c66 a9da381e 6f718583 dce57b9a f2f5d9c9 b336c92f b2df2d43 9083bd1d e33e907b 7fd0fdc7 e5f64db9 b7f0975a>
i want to add F8 to this and send it to server as NSData
NSMutableData *mData = [NSMutableData dataWithData:mData];
unsigned char f8[1] = 0xF8;
[mData appendBytes:f8 length:1];
// You can use the mutable data as is or turn it back into a non-mutable object:
data = [NSData dataWithData:mData];
Related
How to concatenate 3 NSData variables ?
NSData *iv;
NSData *salt;
NSData *encryptedData;
I need to join these to a single variable. Can any one show me a way.
use an NSMutableData object and the method -(void)appendData:(NSData *)otherData
Edited to add example :
NSMutableData *concatenatedData = [NSMutableData data];
[concatenatedData appendData:iv];
[concatenatedData appendData:salt];
[concatenatedData appendData:encryptedData];
// and now you have all of the data in the single variable "concatenatedData"
For those who coding for iOS5 and later.
I'd like to show some real good concatenation. Why are those answers aren't good enough? Because they are involves extra memory usage for copied data. Let's see the answer:
NSMutableData *concatenatedData = [NSMutableData data];
[concatenatedData appendData:iv];
[concatenatedData appendData:salt];
[concatenatedData appendData:encryptedData];
here we have memory allocated for iv, salt and encryptedData
also each time we append one of them to our mutable concatenation we are obviously copy it to mutable data again. Do we want this extra expenses while dealing with large data? Me not.
There is a way to avoid this unnecessary expense of memory - dispatch_data
I'm not going to explain how it works, you can google it if you want.
I just give you a code that works:
NSData *iv = [#"some data" dataUsingEncoding:NSUTF8StringEncoding];
NSData *salt = [#"even more data" dataUsingEncoding:NSUTF8StringEncoding];
NSData *encryptedData = [#"and one more" dataUsingEncoding:NSUTF8StringEncoding];
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_data_t dispatch_data_iv = dispatch_data_create([iv bytes], [iv length], queue, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
dispatch_data_t dispatch_data_salt = dispatch_data_create([salt bytes], [salt length], queue, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
dispatch_data_t dispatch_data_encrypted = dispatch_data_create([encryptedData bytes], [encryptedData length], queue, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
iv = nil; salt = nil; encryptedData = nil; // free all parts, we dont need it anymore
dispatch_data_t dispatch_data_concat = dispatch_data_create_concat( dispatch_data_create_concat(dispatch_data_iv, dispatch_data_salt), dispatch_data_encrypted);
NSData *concatenatedNSData = DataFromDispatchData(dispatch_data_concat);
// lets check now if the concatenation works properly
NSString *stringFromConcatenatedNSData = [[NSString alloc]initWithData:concatenatedNSData encoding:NSUTF8StringEncoding];
NSLog(#"%#",stringFromConcatenatedNSData);
don't forget about the helper-converter
NSData *DataFromDispatchData(dispatch_data_t data)
{
NSMutableData *result = [NSMutableData dataWithCapacity: dispatch_data_get_size(data)];
dispatch_data_apply(data, ^(dispatch_data_t region, size_t offset, const void *buffer, size_t size) {
[result appendBytes:buffer length:size];
return (_Bool)true;
});
return result;
}
You could use NSMutableData's -appendData method:
NSMutableData *result = [NSMutableData data];
[result appendData:iv];
[result appendData:salt];
[result appendData:encryptedData];
// result now has what you need.
This comes at the overhead of using mutable data, which can be slower & use more memory, so use with care. Generally speaking you don't want large NSData's floating around.
First create two NSObjects and use this method
-(void)appendData:(NSData *)otherData
and put in one NSData later with 3rd NSData also concatenate with the same method.
My problem is same as Converting byte array coming from Web service to UIImage iPhone
.Now I am storing these bytes in NSMutableArray.But the method:
NSData *data = [NSData dataWithBytes:YOUR_BYTE_ARRAY length:ARRAY_LENGTH];
takes arrayOfBytes as parameter.So can anyone tell me that how to convert this array in byte array. I searched a lot but unable to find relevant contents.
Not sure how you're getting the mutable array to begin with. If you're using NSURLConnection, the delegate will get NSData, so you needn't use a mutable array. Consider getting the data using the connection asynch block method like this ...
NSURLRequest *myRequest = // the request you've already got working to get image data
[NSURLConnection sendAsynchronousRequest:myRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (!error) {
// image from data with no intermediate mutable array or byte array
UIImage *image = [UIImage imageWithData:data];
}
}];
Thanks for your co-operation. But it does not help me. After a long research i found my solution. I am sharing my information so that others can get right answer.
NSArray *byteArray = [[NSArray alloc]init]; //strore all data here coming from server in byte formate
unsigned c = [byteArray count];
uint8_t *bytes = malloc(sizeof(*bytes) * c);
unsigned i;
for (i = 0; i < c; i++)
{
NSString *str = [byteArray objectAtIndex:i];
int byte = [str intValue];
bytes[i] = (uint8_t)byte;
}
NSData *data = [[NSData alloc]initWithBytes:bytes length:c];
UIImage *image = [UIImage imageWithData:data];
NSLog(#"image %#",image);
My problem is CipherBuffer which is uint8_t i am not able to convert that to NSData and get back the same value.the thing is i need to send the encrypted data to server.
Even i get NSData from serverside and i need to convert that to (unit8_t *). Is their a way to do that i am using below method to decrypt
- (void)decryptWithPrivateKey:(uint8_t *)cipherBuffer plainBuffer:(uint8_t *)decryptedBuffer
To convert uint8_t to NSData
NSData *data = [NSData dataWithBytes:(const void *)encrypted length:bufferSize];
And to convert NSData to uint8_t you can try below method of NSData
- (const void *)bytes;
NSDATA *data1;
NSDATA *data2;
NSDATA *data3;
I want to convert my this all 3 data's to bytearray and then again i want to append it to the
NSMutabledata;
What Should I do For that?
There is no need to convert them to a byte array.
NSMutableData *md = [NSMutableData dataWithData:data1];
[md appendData:data2];
[md appendData:data3];
will give you the data in md.
In an iphone application, I'm looking to convert a float to NSData for it to be sent over bluetooth and then converted back again when it's received. I have the bluetooth part working fine, but when I use this to convert to NSData:
NSData *data = [[NSData alloc]init];
float z = 9.8574; // Get the float value, 9.8574 is just an example
[data getBytes:&z length:sizeof(float)];
I can not convert it back to a float. I've tried a couple of methods but I'm wondering if this is the correct way to encode the float to NSData??
Thanks
Here is how to encode and decode a float with NSData:
encoding:
NSMutableData * data = [NSMutableData dataWithCapacity:0];
float z = ...;
[data appendBytes:&z length:sizeof(float)];
decoding:
NSData * data = ...; // loaded from bluetooth
float z;
[data getBytes:&z length:sizeof(float)];
A couple of things to note here:
1. You have to use NSMutableData if you are going to add things to the data object after creating it. The other option is to simply load the data all in one shot:
NSData * data = [NSData dataWithBytes:&z length:sizeof(float)];
2. the getBytes:length: method is for retrieving bytes from an NSData object, not for copying bytes into it.