Showing posts with label getBytes example. Show all posts
Showing posts with label getBytes example. Show all posts

Saturday, June 1, 2013

NSData getBytes example in Objective C (iOS).


NSData getBytes

Copies a number of bytes from the start of the receiver's data into a given buffer.

- (void)getBytes:(void *)buffer length:(NSUInteger)length

Parameters
buffer
A buffer into which to copy data.
length
The number of bytes from the start of the receiver's data to copy to buffer.

Discussion of [NSData getBytes]
The number of bytes copied is the smaller of the length parameter and the length of the data encapsulated in the object.

NSData getBytes example.
NSData *imageData = UIImagePNGRepresentation(recipeImage.image); 
NSUInteger len = [imageData length];
Byte *byteData= (Byte*)malloc(len);
[imageData  getBytes:byteData length:len];

Example of [NSData getBytes].
NSData *info = [torrent valueForKey:@"info"];
NSData *name = [info valueForKey:@"name"];
unsigned char aBuffer[[name length]];
[name getBytes:aBuffer length:[name length]];
NSLog(@"File name: %s", aBuffer);
..which retrives the data, but seems to have additional unicode rubbish after it:

File name: ubuntu-8.10-desktop-i386.iso)

NSData getBytes example.
you'll probably be able to use something along the following lines to convert into a usable numeric format using getBytes:length: on your NSData object. For e.g.

NSUInteger unencodedInteger;
[myDataObject getBytes:&unencodedInteger length:sizeof(unencodedInteger)];

End of NSData getBytes example article.

Tuesday, May 14, 2013

NSString getBytes example ios


getBytes: maxLength: usedLength: encoding: options: range: remainingRange:

Gets a given range of characters as bytes in a specified encoding.
- (BOOL)getBytes:(void *)buffer maxLength:(NSUInteger)maxBufferCount usedLength:(NSUInteger*)usedBufferCount encoding:(NSStringEncoding)encoding options:(NSStringEncodingConversionOptions)options range:(NSRange)range remainingRange:(NSRangePointer)leftover
Parameters
buffer
A buffer into which to store the bytes from the receiver. The returned bytes are not NULL-terminated.
maxBufferCount
The maximum number of bytes to write to buffer.
usedBufferCount
The number of bytes used from buffer. Pass NULL if you do not need this value.
encoding
The encoding to use for the returned bytes.
options  
A mask to specify options to use for converting the receiver’s contents to encoding (if conversion is necessary).  [NSString getBytes]
range
The range of characters in the receiver to get.
leftover
The remaining range. Pass NULL If you do not need this value.
Return Value
YES if some characters were converted, otherwise NO.
Discussion of [NSString getBytes]
Conversion might stop when the buffer fills, but it might also stop when the conversion isn't possible due to the chosen encoding.
Example of [NSString getBytes]
NSUInteger numberOfBytes = [message lengthOfBytesUsingEncoding:NSUnicodeStringEncoding];
void *buffer = malloc(numberOfBytes);
NSUInteger usedLength = 0;
NSRange range = NSMakeRange(0, [message length]);
BOOL result = [message getBytes:buffer maxLength:numberOfBytes usedLength:&usedLength encoding:NSUnicodeStringEncoding options:0 range:range remainingRange:NULL];
...
free(buffer);
Example of [NSString getBytes]
NSString *src;
char dest[16];
NSUinteger destlen;
[src getBytes:dest
    maxLength:sizeof(dest) - 1
    usedLength:&destlen
    encoding:NSUTF8StringEncoding
    options:0
    range:NSMakeRange(0, [src length])
    remainingRange:NULL];
dest[destlen] = '\0';
Example of [NSString getBytes]
NSUInteger length = [str length];
NSUInteger bufferSize = 500;

char buffer[bufferSize] = {0};

[str       getBytes:buffer
          maxLength:(bufferSize - 1)
         usedLength:NULL
           encoding:NSUTF8StringEncoding
            options:0
              range:NSMakeRange(0, length)
     remainingRange:NULL];