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 9019699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:50:37+00:00 2026-06-16T04:50:37+00:00

NSData * data; NSFileHandle * file; file = [NSFileHandle fileHandleForReadingAtPath: @script.txt]; //this is wrong,

  • 0
NSData * data;
NSFileHandle * file;

file = [NSFileHandle fileHandleForReadingAtPath: @"script.txt"]; //this is wrong, as I have to provide a full pathname, but disregard for now

[file seekToFileOffset: i]; //i not mentioned here, as this is only a snippet of code. Suffice to know that in the file it falls at the beginning of an integer I want to read

data = [file readDataOfLength: 5]; //5-digit integer that I want to read

Now how do I go about casting data as an int which I can use (i.e. for performing arithmetic operations)?

  • 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-06-16T04:50:38+00:00Added an answer on June 16, 2026 at 4:50 am

    Since you are reading your integer from a text file, you can convert it like this:

    char buf[6];
    [data getBytes:buf length:5];
    buf[5] = '\0';
    NSInteger res = atoi(buf);
    

    This assumes an encoding that uses one byte per character, which is consistent with the call of [file readDataOfLength: 5] in the code snippet that you provided.

    If the number of digits is not known ahead of time, you can do this:

    char buf[11]; // Max 10 digits in a 32-bit number + 1 for null terminator
    bzero(buf, 11);
    [data getBytes:buf length:variableLength];
    NSInteger res = atoi(buf);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to check if a file exists. If I use this: NSData *data
I have a comprehension question. This method is given: - (NSArray*)test { NSData *data
I have code like this: NSData *data = [NSData dataWithContentsOfURL:objURL]; const void *buffer =
I have this line of code to convert NSString to NSData: NSData *data =
I tried: NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dictionary]; BOOL success = [data writeToFile:entryPath atomically:YES]; But
I have an application that stores images as NSData in Core Data. I load
I have the following data inside an NSData object: <00000000 6f2d840e 31504159 2e535953 2e444446
Am writing an application for iphone to read a text file using NSData. NSFileHandle
This is the original codes when I am getting this error ClassA.h +(ClassA*)methodA:(NSData*)data; @property
I have an NSData object with a chunk of binary data and I'm trying

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.