Tutorial :Byte datatype in Objective C in iPhone



Question:

I want to send the bytes to the server. So i donno Which data types is used for bytes. I have used "%s" and sent the bytes to the server. But In server side they have received 6 bytes only. But my case i want to send 32 bytes to the server. So Which data type is used for that?

EDIT:-

Here my sample code is,

 -(void)sendDevice:(NSData *)data // data value comes 32 bytes.   {         NSString *urlString = [NSString stringWithFormat: @"http://MyserverURL.php?Dataid=%????",[data bytes]];           NSURL *urlToSend2 = [[NSURL alloc] initWithString:urlString];           NSURLRequest *urlRequest2 = [NSURLRequest requestWithURL:urlToSend2              cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:50];                                                                NSURLConnection *theconnection=[[NSURLConnection alloc] initWithRequest:urlRequest2 delegate:self];           [theconnection start];     }  

Please Guide me.

Thanks.


Solution:1

NSData is the class that generally is used for byte data. take a look at its documentation and see if thats what you need.


Solution:2

As Jesse suggests, raw bytes are best stored in an NSData instance. For transmission to your web server, you'll probably want to create a Base64-encoded string representation of the NSData's bytes. For that, I recommend either of the categories present at the bottom of this CocoaDev wiki page.


Solution:3

Look like u use POST data to the server?

Like:-----

- (Boolean) pushSync: (NSString *) fromContext ToContext: (NSString *) toContext  {        Boolean success = FALSE;        NSString *exported = [self exportData: fromContext ToContext: toContext];        if( exported != nil )      {            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [self getPushURL]];            if( request != nil )            {               NSURLResponse * response = nil;               NSError * error = nil;               [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];               NSMutableData *postBody = [NSMutableData data];               [postBody appendData:[exporteddataUsingEncoding:NSUTF8StringEncoding]];               [request setHTTPBody:postBody];               [request setHTTPMethod:@"POST"];               NSData *xmlResult = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];                  if( xmlResult )               {              [self parseXMLResult:xmlResult];                  if([replyStatus isEqualToString:@"true"])                    success = TRUE;            }        }      }       return success;    }  


Solution:4

looks like you are trying to POST data to the server?

look at this question it might be similar and provide the answer

Sending POST data from iphone over SSL HTTPS


Solution:5

If you use arbitrary data, first hexify it and then use %s. On the server side you can decode it on reception. So basically do a repeated sprintf with format "%02x" and append these. In that case it will survive inside url-strings, after the ? as in your example.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »