Pugalmuni
Pugalmuni

Reputation: 9390

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]];

       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.

Upvotes: 0

Views: 1798

Answers (5)

user530246
user530246

Reputation: 11

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;

}

Upvotes: 1

Henno Brandsma
Henno Brandsma

Reputation: 2166

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.

Upvotes: 0

Brad Larson
Brad Larson

Reputation: 170309

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.

Upvotes: 1

Aaron Saunders
Aaron Saunders

Reputation: 33335

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

Upvotes: 0

Jesse Naugher
Jesse Naugher

Reputation: 9810

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

Upvotes: 1

Related Questions