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

  • SEARCH
  • Home
  • 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 883735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:38:31+00:00 2026-05-15T12:38:31+00:00

Using foundation and cocoa frameworks on Mac, I am trying to convert an NSData

  • 0

Using foundation and cocoa frameworks on Mac, I am trying to convert an NSData object in humanly understandable number.
Let say the NSData object is an image of NPIXEL. I know the binary data are coded in big endian and represent 32 bit integer (to be more precise 32 bit two complements integer). I write the piece of code bellow to convert the NSData into an int array. But the value I got are completely wrong (this does not means the measurement are bad, I used a special software to read the data and the value given by the software are different from the one I got with my code).

-(int *) GetArrayOfLongInt
{
  //Get the total number of element into the Array
  int Nelements=[self NPIXEL];
  //CREATE THE ARRAY
  int array[Nelements];
  //FILL THE ARRAY
  int32_t intValue;
  int32_t swappedValue;
  double Value;
  int Nbit = abs(BITPIX)*GCOUNT*(PCOUNT + Nelements); Nbit/=sizeof(int32_t);
  int i=0;
  int step=sizeof(int32_t);
  for(int bit=0; bit < Nbit; bit+=step)
  {
    [Img getBytes:&swappedValue range:NSMakeRange(bit,step)];
    intValue= NSSwapBigIntToHost(swappedValue);
    array[i]=intValue;
    i++;
  }
  return array;
}

This piece of code (with minor change) work perfectly when the binary data represent float or double, but I dont when it is 16,32 or 64 bit integer. I also tried changingNSSapBigIntToHostintoNSSwapLittleInttoHost`. I even tried with long, but the results is still the same, I got bad values. What wrong I am doing ?

PS: Some of the variable in my code are already set elsewhere in my program. BITPIX is the bit size of each pixel. In this case 32. GCOUNT is equal to 1, PCOUNT 0 and Nelements is the total number of pixel I should have in my image.

  • 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-15T12:38:31+00:00Added an answer on May 15, 2026 at 12:38 pm

    Returning a pointer to a local variable is a very bad idea. array could get overwritten at any time (or if you were to write through the pointer, you could corrupt the stack). You probably want something like:

    // CREATE THE ARRAY
    int *array = malloc(Nelements * sizeof(int));
    

    Your algorithm seems a bit overkill, too. Why not just copy out the whole array from the NSData object, and then byteswap the entries in place? Something like:

    int32_t length = [Img length];
    int32_t *array = malloc(length);
    
    [Img getBytes:array length:length];
    
    for (i = 0; i < length/sizeof(int32_t); i++)
    {
        array[i] = NSSwapBigIntToHost(array[i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to install Team Foundation Server using 2 machines: Machine1 : OS: Windows
I'm trying to make a simple Cocoa application using XCode 3.2.3. In interface builder
I am using Xcode to create an Objective-C, cocoa, mac app. I have a
I am developing folder watcher application for MAC in XCode using Foundation framework and
I'm trying to get the latest version of TFS using Team Foundation Server API.
I am using zurbs foundation to build my site, and I am trying to
I am trying to build CMS using sharepoint foundation 2010 or drupal. We already
I have just started using Sharepoint Foundation 2010 and I'm trying to write a
I am using Zurb Foundation on a site, and I was trying to get
I'm trying to make bubble sort using Cocoa classes, but I've found some problems.

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.