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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:24:28+00:00 2026-05-25T20:24:28+00:00

Im trying to get my data retrieved from a socket into a nsmutablearray. however

  • 0

Im trying to get my data retrieved from a socket into a nsmutablearray. however the examples and tutorials i found showed it going into a string first. which is fine i can parse it out from there. but i cant even get this string thing working.

    case NSStreamEventHasBytesAvailable:
    {

        if(!rawData) {
            rawData = [[NSMutableData data] retain];
        }
        uint8_t buf[1024];
        unsigned int len = 0;
        len = [(NSInputStream *)theStream read:buf maxLength:1024];
        if(len) {
         [rawData initWithBytes:buf length:len];
            int bytesRead;
            bytesRead += len;

            [self messageReceived:rawData];

        } else {
            NSLog(@"no buffer!");
        }
        NSString *str = [[NSString alloc] initWithData:rawData 
                                              encoding:NSUTF8StringEncoding];
        NSLog(@"data buffer: %@ |~|string buffer%@",rawData,str);


        [str release];
        break;
    }

But as you will see from the output below the string never gets any of the data (well actually i think its an encoding problem, and so i think it just looks empty)

2011-09-27 13:14:06.356 Cameleon[30095:207] data buffer: <0f000102>
|~|string buffer

2011-09-27 13:14:06.359 Cameleon[30095:207] data buffer: <02000400
000003> |~|string buffer

2011-09-27 13:14:06.458 Cameleon[30095:207] data buffer: <05000500
00020300> |~|string buffer

2011-09-27 13:14:06.659 Cameleon[30095:207] data buffer: <05000b00
0008080e 13163809 2711> |~|string buffer

2011-09-27 13:14:06.663 Cameleon[30095:207] data buffer: <05000700
00040101 005a> |~|string buffer

i want the string buffer to mirror the values of the databuffer
or an array with each byte of the data buffer

ANSWER:

case NSStreamEventHasBytesAvailable:
        {

            if(!rawData) {
                rawData = [[NSMutableData data] retain];
            }
            uint8_t buf[1024];
            unsigned int len = 0;
            len = [(NSInputStream *)theStream read:buf maxLength:1024];
            if(len) {
             [rawData initWithBytes:buf length:len];                
            } else {
                NSLog(@"no buffer!");
            }

            const uint8_t *bytes = [rawData bytes];

            NSMutableArray *mutableBuffer = [[NSMutableArray alloc] initWithCapacity:len];  

            for (int i =0; i < [rawData length]; i++) {
                [mutableBuffer addObject:[NSString stringWithFormat:@"%02X", bytes[i]]];
            }
            [self gateKeeper:mutableBuffer];


            [mutableBuffer release];
            break;
  • 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-25T20:24:30+00:00Added an answer on May 25, 2026 at 8:24 pm

    Your code has several problems. First off, this is not the usual pattern for allocating an NSData object:

    rawData = [[NSMutableData data] retain];
    

    Although technically correct in terms of memory management, it’s non-idiomatic and results in an unnecessary pair of autorelease and retain messages getting sent. It should instead be this:

    rawData = [[NSMutableData alloc] init];
    

    Secondly, this code is useless:

    int bytesRead;
    bytesRead += len;
    

    You’re declaring a variable, failing to initialize it, adding len to it (which is technically Undefined Behavior, but on x86 this will be harmless), and then doing nothing with it. You probably want to use a longer-lived variable declared outside of this block and initialize it properly.

    Finally, the actual cause of your problem is that the data you’re receiving is not UTF-8 text. It’s some kind of binary data with embedded NUL characters (the zero bytes). When these get converted to strings, the NULs signal termination of the string, so nothing after them gets printed.

    Just keep the data you have as an NSData, don’t bother trying to convert it to a string if it’s not actually textual data. What sort of data are you dealing with? Where is it coming from?

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

Sidebar

Related Questions

What is the best way of handling trying to get data from a DataReader
I'm trying to get character data from www.wowarmory.com using PHP and cURL. The code
for example I'm trying to get the data from database like: using (ExplorerDataContext context
I have sugar crm instance and i was trying to get some data from
I'm trying to use SubSonic with WCF. I can get data into List but
I'm trying to get a simple GXT application to retrieve data from an external
I am trying to pull data from our Oracle datamart into R using RODBC.
I'm trying to create a tab-delimited file using data retrieved from the database. The
I am currently trying to get data out of a plist. It basically looks
I am trying to get my data which is hierarchically set up with a

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.