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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:09:41+00:00 2026-06-05T08:09:41+00:00

While fetching a UTF-8 -encoded file over the network using the NSURLConnection class, there’s

  • 0

While fetching a UTF-8-encoded file over the network using the NSURLConnection class, there’s a good chance the delegate’s connection:didReceiveData: message will be sent with an NSData which truncates the UTF-8 file – because UTF-8 is a multi-byte encoding scheme, and a single character can be sent in two separate NSData

In other words, if I join all the data I get from connection:didReceiveData: I will have a valid UTF-8 file, but each separate data is not valid UTF-8 ().

I do not want to store all the downloaded file in memory.

What I want is: given NSData, decode whatever you can into an NSString. In case the last
few byte of the NSData are an unclosed surrogate, tell me, so I can save them for the next NSData.

One obvious solution is repeatedly trying to decode using initWithData:encoding:, each time truncating the last byte, until success. This, unfortunately, can be very wasteful.

  • 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-05T08:09:42+00:00Added an answer on June 5, 2026 at 8:09 am

    If you want to make sure that you don’t stop in the middle of a UTF-8 multi-byte sequence, you’re going to need to look at the end of the byte array and check the top 2 bits.

    1. If the top bit is 0, then it’s one of the ASCII-style unescaped UTF-8 codes, and you’re done.
    2. If the top bit is 1 and the second-from-top is 0, then it the continuation of an escape sequence and might represent the last byte of that sequence, so you will need to buffer the character for later and then look at the preceding character*
    3. If the top bit is 1 and the second-from-top is also 1, then it is the beginning of the multi-byte sequence and you need to determine how many characters are in the sequence by looking for the first 0 bit.

    Look at the multi-byte table in the Wikipedia entry: http://en.wikipedia.org/wiki/UTF-8

    // assumes that receivedData contains both the leftovers and the new data
    
    unsigned char *data= [receivedData bytes];
    UInteger byteCount= [receivedData length];
    
    if (byteCount<1)
        return nil;  // or @"";
    
    unsigned char *lastByte = data[byteCount-1];
    if ( lastByte & 0x80 == 0) {
        NSString *newString = [NSString initWithBytes: data length: byteCount 
                                        encoding: NSUTF8Encoding];
        // verify success
        // remove bytes from mutable receivedData, or set overflow to empty
        return newString;
    }
    
    // now eat all of the continuation bytes
    UInteger backCount=0;
    while ( (byteCount > 0)  && (lastByte & 0xc0 == 0x80)) {
        backCount++;
        byteCount--;
        lastByte = data[byteCount-1];
    }
    // at this point, either we have exhausted byteCount or we have the initial character
    // if we exhaust the byte count we're probably in an illegal sequence, as we should 
    // always have the initial character in the receivedData
    
    if (byteCount<1) {
        // error!
        return nil;
    }
    
    // at this point, you can either use just byteCount, or you can compute the 
    // length of the sequence from the lastByte in order
    // to determine if you have exactly the right number of characters to decode UTF-8.
    
    UInteger requiredBytes = 0;
    if (lastByte & 0xe0 == 0xc0) {  // 110xxxxx
        // 2 byte sequence
        requiredBytes= 1;
    } else if (lastByte & 0xf0 == 0xe0) {   // 1110xxxx
        // 3 byte sequence
        requiredBytes= 2;
    } else if (lastByte & 0xf8 == 0xf0) {   // 11110xxx
        // 4 byte sequence
        requiredBytes= 3;
    } else if (lastByte & 0xfc == 0xf8) {   // 111110xx
        // 5 byte sequence
        requiredBytes= 4;
    } else if (lastByte & 0xfe == 0xfc) {   // 1111110x
        // 6 byte sequence
        requiredBytes= 5;
     } else {
        // shouldn't happen, illegal UTF8 seq
     }
    
     // now we know how many characters we need and we know how many
     //  (backCount) we have, so either use them, or take the 
     // introductory character away.
     if (requiredBytes==backCount) {
         // we have the right number of bytes
         byteCount += backCount;
     } else { 
         // we don't have the right number of bytes, so remove the intro character 
         byteCount -= 1;   
     }
    
     NSString *newString = [NSString initWithBytes: data length: byteCount 
                                     encoding: NSUTF8Encoding];
     // verify success
     // remove byteCount bytes from mutable receivedData, or set overflow to the 
     // bytes between byteCount and [receivedData count]
     return newString;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Prestashop. I am getting trouble while fetching all images for a
i have a hibernate class: class student{ string name; int roll; } while fetching
I am using simple html dom to fetch datas from other websites. while fetching
i am having problems while fetching data from kml file. geoXml.parse(http://localhost/maps/phpmysqlajax_genkml.kml); error in console
I am facing a problem while fetching values using paginate function in cakephp. In
Is there a way to limiting while loops when fetching data from mysql ?
I am not getting response while fetching result from my code using AJAX. while
I am facing one problem while fetching a file which is downloaded from an
I have a simple function that shows loading spinner while fetching data (usually takes
While making some final tests of a class-library that I'm writing for Windows Mobile

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.