Testing the relationship between NSData, NSMutableData And bytes method and Byte Type variables want to change NSData Value to Bytes, when i run this it crashes the app but doesnt throw any error..
This Runs Ok
NSData *myData = [[NSData alloc] initWithData:someData];
Byte *finalValue = (Byte *)[myData bytes];
But This throws crashes app and doesnt throw an error
NSData *myData = [[NSData alloc] initWithData:someData];
NSMutableData *testingWaters = (NSMutableData *)[myData bytes];
Byte *finalValue = (Byte *)[testingWaters bytes];
EDITED: Keep In mind i want to convert a NSData Variable or NSMutableData Variable into a Byte variable.
You can create a mutable copy of myData
NSData* someData = [[NSString stringWithFormat:#"HELLO WORLD"]dataUsingEncoding:NSUTF8StringEncoding];
NSData *myData = [[NSData alloc] initWithData:someData];
NSMutableData *testingWaters = (NSMutableData *)[myData mutableCopy];
Byte *finalValue = (Byte *)[testingWaters bytes];
Related
I have the following code and I am getting an exception with it.
-[_NSInlineData replaceBytesInRange:withBytes:]: unrecognized selector sent to instance 0x6000027ff2e0'
terminating with uncaught exception of type NSException
Can someone point out the issue with this?
NSMutableData *data = [[NSMutableData alloc]
initWithLength:1000];
NSMutableData *d1 =(NSMutableData *) [data
subdataWithRange:NSMakeRange(sizeof(uint8_t),10)];
uint8_t i = 10;
[d1 replaceBytesInRange: NSMakeRange(1, sizeof(uint8_t)
withBytes:&i];
subdataWithRange: returns an immutable NSData instance. It's a copy of the original data. If you want to replace data in that copy, without affecting your original data object, you can do:
NSData *tmp = [data subdataWithRange:NSMakeRange(sizeof(uint8_t),10)];
NSMutableData *d1 = [tmp mutableCopy];
If you want to modify the mutable data object instead, do so directly by calculating the correct range:
// Offset derived from your example code, lines 5 and 7.
NSUInteger offset = sizeof(uint8_t) + 1;
NSRange range = NSMakeRange(offset, sizeof(uint8_t));
[data replaceBytesInRange:range withBytes:&i];
I use below code to encode and decode a string on objective C. The encoding is good, I debug and see that it throw a hash string when input is #"1". But when I try to decode this hash string, it return nil.
Please help me.
+(NSString *)encrypt: (NSString*) input
{
//Base64 Encoding
char base64Result[32];
size_t theResultLength = 32;
Base64EncodeData(input, 20, base64Result, &theResultLength);
NSData *theData = [NSData dataWithBytes:base64Result length:theResultLength];
NSString *base64EncodedResult = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
NSString* decryptedString = [self decrypt:base64EncodedResult];
return [base64EncodedResult autorelease];
}
+ (NSString *) decrypt:(NSString*) input{
Byte inputData[[input lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];//prepare a Byte[]
[[input dataUsingEncoding:NSUTF8StringEncoding] getBytes:inputData];//get the pointer of the data
size_t inputDataSize = (size_t)[input length];
size_t outputDataSize = EstimateBas64DecodedDataSize(inputDataSize);//calculate the decoded data size
Byte outputData[outputDataSize];//prepare a Byte[] for the decoded data
Base64DecodeData(inputData, inputDataSize, outputData, &outputDataSize);//decode the data
NSData *theData = [[NSData alloc] initWithBytes:outputData length:outputDataSize];//create a NSData object from the decoded data
NSString *result = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
return [result autorelease];
}
In you encoding method, you have to convert the input string to a byte buffer and feed that to Base64EncodeData:
NSData *inputData = [input dataUsingEncoding:NSUTF8StringEncoding];
Base64EncodeData([inputData bytes], [inputData length], base64Result, &theResultLength, NO);
(The NSString *input argument in the encoding method points to an Objective-C structure, not to a C string. So your encoding method seems to work. It encodes something, but not the input string. The decoding method then fails at
NSString *result = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
because the decoded data does not contain valid UTF-8.)
I'm trying to convert an NSString to uint8_t. The problem I'm having is that the NSString and the resulting uint8_t variables do not match. Here is some example code:
NSLog(#"Key now: %#", key);
NSData* keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
const uint8_t *plainBuffer = (const uint8_t*)[data bytes];
size_t plainBufferSize = strlen((char *) plainBuffer);
NSLog(#"Plain buffer: %s", plainBuffer);
NSData* testData = [[NSData alloc] initWithBytes: plainBuffer length:plainBufferSize];
NSString* testString = [[NSString alloc] initWithData: testData encoding: NSUTF8StringEncoding];
NSLog(#"Test string: %#", testString);
And example output:
Key now: 9iIWBpf5R6yu5pJ93l218RsMdWBLidXt
Plain buffer: 9iIWBpf5R6yu5pJ93l218RsMdWBLidXtMdWBLidXt
Test string: 9iIWBpf5R6yu5pJ93l218RsMdWBLidXtMdWBLidXt
Clearly its the NSData -> uint8_t step thats going wrong, but I don't know why!
You're using strlen() to get the size of an NSData*. That's not going to work. The NSData* isn't NUL-terminated. So you're getting garbage on the end.
Don't use strlen(). Just ask the NSData* for its size directly.
Alternatively, don't use NSData* at all and just ask for [key UTF8String]. That hands back a NUL-terminated const char *.
I'm confused on how to store a hex value into NSData. I want the value I will be storing to be 0x0F.
Please could someone give me an example?
You can create an array to hold your hex values and then use that to create the data:
unsigned char bytes[] = {0x0F};
NSData *data = [NSData dataWithBytes:bytes length:1];
This can be rewritten as:
NSData *data = [NSData dataWithBytes:(unsigned char[]){0x0F} length:1];
Here's how I've done that before:
UInt8 j= 0x0f;
NSData *data = [[[NSData alloc] initWithBytes:&j length:sizeof(j)] autorelease];
I'm trying to encode and decode base64 data. but while decoding the base64 data, it returns bunch of hex values, but i couldn't display or printout using NSlog to the original readable strings. The below code couldn't print anything, just empty.
Can anyone help ? thanks
>
>
NSString* msgEncoded = [[NSString alloc] initWithFormat:#"Q1NNKE1DTC9TTUEgUkNWL2FkbWluQHNldGVjcy5jb20gT1JHLyBUVkIvNDNkYzNlMzQwYWQ3Yzkp:"];
NSData* decoded = [[NSData alloc] initWithData:[self decodeBase64WithString:msgEncoded]];
NSString* plainString = [[NSString alloc]initWithData:decoded encoding:NSUTF8StringEncoding];
NSLog(#"\n Decoded string: %# \n", plainString );
There is a built in function in NSData
[data base64Encoding];
[data base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
If you are still having issues, try out this library: https://github.com/l4u/NSData-Base64
use it like so:
#import "NSData+Base64.h"
NSData *someData //load your data from a file, url or photo as needed
NSData *file = [NSData dataWithContentsOfFile:#"mytextfile.txt"];
NSData *photo = UIImageJPEGRepresentation(self.photo.image,1);
//encode it
NSString *base64string = [photo base64EncodedString];
NSString *base64file = [file base64EncodedString];
//decode it
NSData *back = [NSData dataFromBase64String:base64string];
Try Google's GTMStringEncoding class. You'll need GTMDefines.h too.
GTMStringEncoding *coder = [GTMStringEncoding rfc4648Base64StringEncoding];
NSString *encodedBase64 = [coder encodeString:#"Mary had a little lamb"];
// will contain the original text
NSString *decodedText = [coder decodeString:encodedBase64];
To encode NSData* to NSString* and back to NSData*, use the encode: + decode: methods instead of encodeString: + decodeString:.
As a bonus you get a lot of additional useful encodings, such as the url-safe variant of Base64.