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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:24:36+00:00 2026-05-29T04:24:36+00:00

Using the HTML5 File API I can get the Binary String representation of a

  • 0

Using the HTML5 File API I can get the Binary String representation of a file using FileReader.readAsBinaryString(). For an image this might return: “”ÿØÿàJFIFHHÿáExifMM*bj(1r2i¤ÐHHAdobe Photoshop CS Windows2006:07:12 15:24:47 ÿÿ…”. Using Objective-C, I’m trying to get the same representation of a file.

NSData *data = [NSData dataWithContentsOfFile:path];
unsigned char *aBuffer = malloc([data length]);
[data getBytes: aBuffer length:[data length]];
return [[NSString alloc] initWithBytes:aBuffer length:[data length] encoding:NSASCIIStringEncoding];

The above has two issues (at least), there is a memory leak and the output is only ‘ÿØÿà’. How do I get the full string and avoid a memory leak?

Edit when I do this:

NSData *data = [NSData dataWithContentsOfFile:path];
return [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSASCIIStringEncoding];

I get the exact same output.

WebKit does the following (in C++):

 m_stringResult = String(static_cast<const char*>(m_rawData->data()), m_bytesLoaded);

That is within FileReaderLoader.cpp, line 252. It is within WebCore, which is within WebKit.

  • 1 1 Answer
  • 1 View
  • 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-29T04:24:37+00:00Added an answer on May 29, 2026 at 4:24 am

    For the memory leak: have you heard of -autorelease?

    return [[[NSString alloc] initWith...] autorelease];
    

    For the core problem:

    NSString assumes to get a C string as its “bytes” parameter. So it will only get the file contents up to a 0x00 byte, which it interprets as a ‘terminating NUL’ character.
    By the way, the correct method to do it is not forcing a poor NSString instance to initialize itself with invalid (non-ASCII, non-string!) garbage. You’d better use NSData’s -bytes method to get a byte array. You can even access its individual bytes using something like this:

    int index = [data lenght] - 1;
    char byteAtIndex = ((char *)[data bytes])[index];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone give me an example of using the FileReader API go get contents
I am trying to create a chrome extension using HTML5 WebSockets and can't get
Using the HTML5 Geolocation API I've ended up with some variables in Javascript that
I'm using Java XPath API to extract content from a xhtml file. I'm pasring
I'm using the following jQuery code to get a JSON file from CouchDB. Function
I'm using simpleXML to parse this xml file . It's a feed I'm using
Can we integrate the FileAccess API of HTML with normal download of Files using
...when I can access the data using the direct URL » http://www.tumblr.com/api/likes?email=myemail@gmail.com&password=mypassword&debug=1 , which
I'd like to extract the text from an HTML file using Python. I want
I'm using the jquery library to load the content of an html file. Something

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.