Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7573141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:06:23+00:00 2026-05-30T16:06:23+00:00

I have a NSData object coming back from my server, it varies in its

  • 0

I have a NSData object coming back from my server, it varies in its content but sticks to a particular structure.

I would like to know (hopfully with some example code) how to work though this object to get the data I need out of it.

the structure of the data objects inside the objects are like this

  • leading value (UInt16) – (tells me what section of the response it is)
  • Size of string (UInt32) or number – (UInt32)
  • String (not null terminated) i.e. followed by the next leading value.

I have been reading through the Binary Data Programming Guide however that’s only really showing me how to put my data into new NSData objects and accessing and compairing the bytes.

The thing I am stuck on is how do I say grab the info dynamically. Check the NSdata objects first leading value figure out if its string or int then get the string or int and move onto the next leading value..

any suggestions or example code would be really helpfull.. just stuck in abit of a mind block as I have never attempted anything like this in objective C.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:06:24+00:00Added an answer on May 30, 2026 at 4:06 pm

    Some of this depends on how your server is written to encode the data into what it is sending you. Assuming it is encoding the numeric values using standard network byte ordering (big-endian) you will want it converted to the correct byte-ordering for iOS (I believe that is always little-endian).

    I would approach it something like this:

    uint16_t typeWithNetworkOrdering, typeWithLocalOrdering;
    uint32_t sizeWithNetworkOrdering, sizeWithLocalOrdering;
    char *cstring = NULL;
    uint32_t numberWithNetworkOrdering, numberWithLocalOrdering;
    const void *bytes = [myData bytes];
    NSUInteger length = [myData length];
    
    while (length > 0) {
        memcpy(&typeWithNetworkOrdering, bytes, sizeof(uint16_t));
        bytes += sizeof(uint16_t);
        length -= sizeof(uint16_t);
        memcpy(&sizeWithNetworkOrdering, bytes, sizeof(uint32_t));
        bytes += sizeof(uint32_t);
        length -= sizeof(uint32_t);
        typeWithLocalOrdering = CFSwapInt16BigToHost(typeWithNetworkOrdering);
        sizeWithLocalOrdering = CFSwapInt32BigToHost(sizeWithNetworkOrdering);
    
        if (typeWithLocalOrdering == STRING_TYPE) { // STRING_TYPE is whatever type value corresponds to a string
            cstring = (char *) malloc(sizeWithLocalOrdering + 1);
            strncpy(cstring, bytes, sizeWithLocalOrdering);
            cstring[sizeWithLocalOrdering] = '\0';
            NSString *resultString = [NSString stringWithCString:cstring encoding:NSUTF8StringEncoding];
            NSLog(@"String = %@", resultString);
            free(cstring);
            bytes += sizeWithLocalOrdering;
            length -= sizeWithLocalOrdering;
            // Do whatever you need to with the string
        }
        else if (typeWithLocalOrdering == NUMBER_TYPE) { // NUMBER_TYPE is whatever type value corresponds to a number
            memcpy(&numberWithNetworkOrdering, bytes, sizeof(uint32_t));
            numberWithLocalOrdering = CFSwapInt32BigToHost(numberWithNetworkOrdering);
            NSLog(@"Number = %u", numberWithLocalOrdering);
            bytes += sizeof(uint32_t);
            length -= sizeof(uint32_t);
            // Do whatever you need to with the number
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have UTF-8 encoded NSData from windows server and I want to convert it
I have seen this done the other way. But I have a NSData object
I'm grabbing song data from the iOS music library (as an NSData object), but
I have an NSData object of approximately 1000kB in size. Now I want to
I have a NSData of bytes from where I want to subtract a string.
I have to convert an NSDictionary object into NSData and further I have to
In my iPhone project,I need to convert NSData object to file.I have to convert
i have converted my .caf audio file to NSData object using the following line
I have a binary file I've loaded using an NSData object. Is there a
I would like to set the value of an NSData object within my block.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.