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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:21:34+00:00 2026-05-27T20:21:34+00:00

How can I return back to an NSString from the byte date of the

  • 0

How can I return back to an NSString from the byte date of the NSString containing this character: fl ?

NSString *inflatedString01  = @"fl";

// original code that was the problem!
NSData *dataOfString = [NSData dataWithBytes:[inflatedString01 UTF8String] length:[inflatedString length]];
// code that fixes the problem
//NSData *dataOfString = [inflatedString01 dataUsingEncoding:NSUTF8StringEncoding]; //thanks zneak

NSLog(@"%@",inflatedString01);
NSLog(@"%i",[inflatedString01 length]);
NSLog(@"%@",dataOfString);
NSLog(@"%i",[dataOfString length]);

NSString *stringFromData = [NSString stringWithCString:[dataOfString bytes] encoding:NSUTF8StringEncoding]

NSLog(@"%@",stringFromData);

The output of the above gives:

2012-01-02 08:47:48.963 TestApp[74363:fe03] fl
2012-01-02 08:47:49.262 TestApp[74363:fe03] 1
2012-01-02 08:47:49.540 TestApp[74363:fe03] <ef>
2012-01-02 08:47:49.924 TestApp[74363:fe03] 1
2012-01-02 08:47:50.787 TestApp[74363:fe03] (null)

I’d like to see fl instead of (null) for the last NSLog output. I’m guessing there is a significance with the ‘ef’ output of NSData.

  • 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-27T20:21:35+00:00Added an answer on May 27, 2026 at 8:21 pm

    The problem is the discrepancy of the character size versus the binary size. When you call [NSString length], what you get is the number of logical characters in the string, not the number of bytes required to store it in an arbitrary encoding. The character fl is one logical character for the NSString class, but its UTF-8 encoding is ef ac 82: it takes up 3 bytes.

    Your call to [NSData dataWithBytes:length:] receives a pointer to these 3 bytes, but then [inflatedString01 length] tells that it’s only one character and you pass that as the number of bytes; this is why your data contains only ef.

    strlen, not being encoding-aware, will just count the number of bytes in a C string until it finds a zero, so it will accurately return the number of bytes in an UTF-8 string (as you noted in your comment).

    The best solution would probably to simply call [inflatedString01 dataUsingEncoding:NSUTF8StringEncoding] to get the bytes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that I can return the index of a particular character of a
Also, can you SEND any header back? (return headers) when you run a web
How can return a variable from a thead (I have the threads handle too).
I have a method that can return either a single object or a collection
I need a function that can return the difference between the below two dates
I was wondering if I can return an error callback back to my jquery
I still need your help. I have this piece of code that doesn't want
been having this problem for a few days now and can't seem to find
How can return a subset of a FormCollection with key/values in tact for those
What query can return the names of all the stored procedures in a SQL

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.