Byte datatype in Objective C in iPhone
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]];
开发者_如何学PythonNSURL *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.
NSData
is the class that generally is used for byte data. take a look at its documentation and see if thats what you need.
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.
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;
}
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
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.
精彩评论